-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: PHPDoc types in controller.tpl.php #8561
fix: PHPDoc types in controller.tpl.php #8561
Conversation
* Return the properties of a resource object | ||
* Return the properties of a resource object. | ||
* | ||
* @param string|null $id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it not included int
? My idea is that if the link is exm.dev/show/5 then @param int|string|null $id
should be defined.
I have the same opinion for other cases including edit()
, editable()
and...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, there is no way to pass an int parameter now.
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -5,4 +5,4 @@ use CodeIgniter\Router\RouteCollection;
/**
* @var RouteCollection $routes
*/
-$routes->get('/', 'Home::index');
+$routes->get('/(:num)', 'Home::index/$1');
diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
index 5934333309..ec6d8774d4 100644
--- a/app/Controllers/Home.php
+++ b/app/Controllers/Home.php
@@ -4,8 +4,8 @@ namespace App\Controllers;
class Home extends BaseController
{
- public function index(): string
+ public function index($id): string
{
- return view('welcome_message');
+ dd($id);
}
}
Navigate to http://localhost:8080/123
:
$id string (3) "123"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<?php
namespace App\Controllers;
class Home extends BaseController
{
public function index(int $id):string
{
dd($id);
}
}
Output:
$id integer 10
Sorry, I thought these two were the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@datamweb that will be fatal error on declare(strict_types=1)
iirc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, i was looking returning instead of just passing the param type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably @param mixed id
is possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@datamweb That will be fatal error if we put declare(strict_types=1)
in the code that calls the controller, that is CodeIgniter.php
.
PHP originally all values coming from the outside are strings. Therefore, CI does not currently provide a way to type controller parameters.
However, I think it is reasonable to want to specify an int parameter in the controller. Also, CI does not set strict types at the moment, so it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably
@param mixed id
is possible?
As far as I know, in CodeIgniter4 tries not to use mixed
. And I like the effort.
Guys thanks for your explanation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's include int
. That's how Laravel allows type-casting on controller parameters so some people may be familiar with it already. I actually really like their routing parameter constraints feature (https://laravel.com/docs/10.x/routing#parameters-regular-expression-constraints) which saves a lot of steps when your resource routes all us an int
ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added int
for $id
.
Now if we add type int to controller params, it works. So we must allow type-casting on controller parameters in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Description
See #6310
Ref #8144
Checklist: