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

2.5 - requestAction routes to wrong URL when prefixes are set. #3443

Closed
thinkingmedia opened this issue May 6, 2014 · 5 comments
Closed

Comments

@thinkingmedia
Copy link
Contributor

Assume you have a prefix set for json for a controller's action called index. If you use requestAction during that request it will have the json prefix set, and it shouldn't.

Assuming you're using the default routes from CakePHP.

Example controller

controller DocumentsController extends AppController {
       public json_index() {
           // json is a prefix
       }
}

contoller UsersController extends AppController {
       public find() {
           // has no prefix
       }
}

Now in the view json_index.ctp

 $data = $this->requestAction(array('controller'=>'users','action'=>'find'));

The above will generate the error /users/json_find action does not exist in Users controller.

This is a problem in a couple of ways. 1) The programmer has to add 'json'=>false to all requestAction calls. 2) If the programmer adds a new prefix all requestAction calls will break if the request has it set.

@dereuromark
Copy link
Member

Maybe your expectation is wrong.
When setting an extension, it should probably keep it unless told otherwise.
Currently, the same is true for "admin" and "plugin" prefixed by the way.
They stay also the same unless explicitly changed or set to "false".

@dereuromark dereuromark added this to the 2.5.0 milestone May 6, 2014
@thinkingmedia
Copy link
Contributor Author

The most common place to use requestAction is in an element, and elements are intended for re-use. They shouldn't be tightly coupled to the current type of request being made.

An element that uses requestAction should only be coupled with the controller/action that its calling. How can an element know what current prefix is set?

I've run into this issue when rendering error handlers as an example. Errors need to generate their view in a wide range of requests types.

@markstory
Copy link
Member

The issue here is that all reverse routed URLs inherit the the current prefix as the common case is generating URLs for redirects and links. A simple way around this issue is to use string URLs. Unlike 1.x, array URLs are less efficient than string URLs when used with requestAction() in 2.x.

@thinkingmedia
Copy link
Contributor Author

Thanks Mark,

I didn't know strings would resolve the issue. That's good for me. I'll change my requests to use strings from now on.

@markstory
Copy link
Member

Awesome, I'm going to close this as doing deeper surgery on the Router to handle this case will probably cause more trouble than it is worth. It may also break existing apps where people want the prefix to automatically apply itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants