Skip to content
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

Merged
merged 5 commits into from
Feb 23, 2024

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Feb 19, 2024

Description
See #6310

  • fix/add PHPDoc types

Ref #8144

Checklist:

  • Securely signed commits
  • [] Component(s) with PHPDoc blocks, only if necessary or adds value
  • [] Unit testing, with >80% coverage
  • [] User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Feb 19, 2024
* Return the properties of a resource object
* Return the properties of a resource object.
*
* @param string|null $id
Copy link
Contributor

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...

Copy link
Member Author

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"

Copy link
Contributor

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.

Copy link
Member

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

Copy link
Member

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

Copy link
Member

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?

Copy link
Member Author

@kenjis kenjis Feb 23, 2024

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.

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Member Author

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.
Copy link
Contributor

@datamweb datamweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kenjis kenjis merged commit 3c5a22e into codeigniter4:develop Feb 23, 2024
62 checks passed
@kenjis kenjis deleted the fix-types-in-controller.tpl.php branch February 23, 2024 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants