cakephp: UrlHelper can't match with any defined routes if missing _method option
This is a (multiple allowed):
-
bug
-
enhancement
-
feature-discussion (RFC)
-
CakePHP Version: 3.5.6
-
Platform and Target: PHP built-in webserver
What you did
In routes.php I declared a route like this:
$routes->connect('/login', ['controller' => 'Users', 'action' => 'login'], ['_name' => 'login'])->setMethods(['GET', 'POST']);
In the template file, I uses UrlHelper like this:
$this->Url->build(['_name' => 'login'])
What happened
I get Cake\Routing\Exception\MissingRouteException
and an error
Warning (2): strtoupper() expects parameter 1 to be string, array given [CORE/src/Routing/Route/Route.php, line 744]
What you expected to happen
It should match with the route that I have declared in routes.php and return /login string.
The error will gone if I’m also add _method option like this:
$this->Url->build(['_name' => 'login', '_method' => 'GET'])
I think it should not matter what http method will be used the url to be generated is /login
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (16 by maintainers)
Commits related to this issue
- Add a failing test. Verifies the existence of the issue mentioned here: https://github.com/cakephp/cakephp/issues/11467#issuecomment-423275388 — committed to beporter/cakephp by beporter 6 years ago
- Add a failing test. Verifies the existence of the issue mentioned here: https://github.com/cakephp/cakephp/issues/11467#issuecomment-423275388 — committed to beporter/cakephp by beporter 6 years ago
@beporter Any chance you could put a pull request together with that change?
I can care to this
@saeideng I don’t use custom route class. I use built-in
DashedRouteclass. @markstory_methodbecome an array because:->setMethods(['GET', 'POST'])inroutes.php. It will set default method to an array['GET', 'POST']._methodoption when callingUrl::build(). I don’t provide it because I use named route, that what I expect is only need to pass the_nameoption._methodwhich is an array. Here is var_dump output of$route->defaults