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

feat: can add route handler as callable #5713

Merged
merged 4 commits into from
Feb 25, 2022

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Feb 20, 2022

Description

  • You can jump to the controller method with IDE.

E.g.:

$routes->get('home', [Hello::class, 'index']);
$routes->get('product/(:num)/(:num)', [[Hello::class, 'index'], '$1/$2']);

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 enhancement PRs that improve existing functionalities label Feb 20, 2022
@mostafakhudair
Copy link
Contributor

Please include additions in the user guide

@kenjis kenjis force-pushed the feat-route-add-callable branch from 9c11934 to 77a0302 Compare February 20, 2022 23:35
@iRedds
Copy link
Collaborator

iRedds commented Feb 20, 2022

[[Home::class, 'index'], '$1/$2'] doesn't look nice

What about?

//[Home::class, 'index', '$1/$2'] 

[$class, $method, $params] = $to; 
is_callable([$class, $method], true, $callableName)

//... 

@kenjis
Copy link
Member Author

kenjis commented Feb 21, 2022

[Home::class, 'index', '$1/$2'] seems to be nice.
But we cannot jump to Home::index() source code when we click 'index'

Screenshot 2022-02-21 9 18 29
Screenshot 2022-02-21 9 18 44

@iRedds
Copy link
Collaborator

iRedds commented Feb 21, 2022

Yes, you are right.

Copy link
Member

@paulbalandan paulbalandan left a comment

Choose a reason for hiding this comment

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

This needs writeup in the docs.

@iRedds
Copy link
Collaborator

iRedds commented Feb 22, 2022

In theory. Can we drop $1/$2?

@kenjis
Copy link
Member Author

kenjis commented Feb 22, 2022

I will write the docs later.

@kenjis kenjis force-pushed the feat-route-add-callable branch from 77a0302 to e7230dc Compare February 23, 2022 00:30
@kenjis
Copy link
Member Author

kenjis commented Feb 23, 2022

Added User Guide.

@kenjis kenjis force-pushed the feat-route-add-callable branch from e7230dc to 115fc77 Compare February 23, 2022 01:53
@iRedds
Copy link
Collaborator

iRedds commented Feb 23, 2022

@kenjis can you reply to the comment above?
I want to clarify. I meant that $1/$2 is useless in my opinion.

$routes->get('product/(:num)/(:num)', [Hello::class, 'index']);

Here we already have two arguments (:num)/(:num). Which can be passed to a controller method without the $1/$2 notation.
The same principle as the closure works.

$routes->get('product/(:num)/(:num)', fn ($param1, $param2) => 'do something');

i.e. we don't specify $1/$2 and parameters are passed as is.

@lonnieezell
Copy link
Member

This looks like a great addition. I would be in favor of removing the $1/$2 params as being specifically needed. While I've had times in the past where I needed to reorder or leave out some parameters, it's never been a big enough deal that it couldn't be worked around, and would clean this up a bit. Maybe make it optional? If it's not provided it defaults to the order given?

@iRedds
Copy link
Collaborator

iRedds commented Feb 23, 2022

To skip the parameter, the developer can use non-capturing grouping.
For example, for the url product/1/2 and the route product/(?::num)/(:num), the parameter will only be 2.

@kenjis kenjis force-pushed the feat-route-add-callable branch from 115fc77 to b2fb66d Compare February 23, 2022 06:49
@kenjis
Copy link
Member Author

kenjis commented Feb 23, 2022

@iRedds @lonnieezell how about this?

@iRedds
Copy link
Collaborator

iRedds commented Feb 23, 2022

It will not work correctly with a non-capturing group.
For product/(?::num)/(:num) the replacement string will be controller::method/$1/$2 and we get the string controller::method/1/ which gives parameter [1, ''].
Perhaps with nested groups there will be a similar situation.

It seems to me that if you use your approach, then you need to trim the slash in Router::checkRoutes().
Or you can add parameters directly to Router::checkRoutes()

} elseif (strpos($val, '$') === false && count($matches) > 1) {
    array_shift($matches);
    $val.= '/' . implode('/', $matches);
}

$this->setRequest(explode('/', $val));

This solution will also allow parameters to be applied to more than just the $to array.
For example, $routes->get('x/(:num)', 'Home::index');
But this is a quick example and I could miss the nuances.

@kenjis
Copy link
Member Author

kenjis commented Feb 23, 2022

I think regex is originally complex, and we do not need to support all the pattern.

  • do not support non-capturing groups
  • do not support nested groups

Or I don't mind to change the implementation if there is simple better one.

But if $routes->get('x/(:num)', 'Home::index'); will add params automatically,
it is a breaking change. So it needs to be considered more.

E.g.:
$routes->add('home', [Hello::class, 'index']);
$routes->add('product/(:num)/(:num)', [[Hello::class, 'index'], '$1/$2']);
@kenjis kenjis force-pushed the feat-route-add-callable branch from b2fb66d to 464ed03 Compare February 25, 2022 04:26
@kenjis
Copy link
Member Author

kenjis commented Feb 25, 2022

Copy link
Member

@lonnieezell lonnieezell left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks!

system/Router/RouteCollection.php Outdated Show resolved Hide resolved
system/Router/RouteCollection.php Outdated Show resolved Hide resolved
@kenjis
Copy link
Member Author

kenjis commented Feb 25, 2022

@paulbalandan The commit you approved failed tests.
Fixed.

@kenjis kenjis requested a review from paulbalandan February 25, 2022 08:59
@kenjis kenjis merged commit 196d759 into codeigniter4:develop Feb 25, 2022
@kenjis kenjis deleted the feat-route-add-callable branch February 25, 2022 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants