I have my own routing rules in routes.php, defined for all the pages that should be accessible via URL, such as mywebsite/blog/ and mywebsite/blog/category/category-name, i.e. the structure of my whole website is covered by my custom routes.
Now, I have a lot of elements that make use of requestAction, such as
$websiteabstract = $this -> requestAction(array(
'controller' => 'assets',
'action' => 'displayHomeAbstract'
));
This gives me an error Error: Controller could not be found, probably because I have not defined a route for /assets/displayHomeAbstract. But why do I have to define a custom route for that, when I explicitly state the name of the controller and the action? Shouldn't that bypass the routing altogether?
Either I have not understand Routing at all. Or do I really have to define ALL the possible routes (even those that are only used by requestAction) in my routes.php? I mean, I don't want to allow users to directly access mywebsite/assets/displayHomeAbstract anyway, only via an Element.
Thank you
EDIT: Here is my routes.php http://pastebin.com/aAKBwNZJ
Please have a look at line 128, this is exactly what I do not want since /assets/displayHomeAbstract is ONLY accessed via requestAction.
EDIT: And this is the element, that makes the request: http://pastebin.com/0tK5dYJk
Okay, after extensive discussion with the devs in IRC, I think I understand this well enough to explain to you:
You do have to define your custom routes for your requestAction in this case. requestAction is emulating a full request. It dispatches a request as if accessed using the string url every time, even when the url provided is an array. The book is referring to how when you have a custom route defined in addition to using the default routes (the last line of routes.php), you can use array urls to be agnostic of those routes. However, these array urls rely on the default routes.php in the /lib/ folder and are used to construct a url string. If you're going to have a custom routing pattern, you have to construct the url strings on your own.
Note: the comments below were from earlier versions of this answer.
The key to your problem is understanding the scope of Cake's routing and how it work.
When you define a route in CakePHP, it isn't just used for mapping URLs to controllers. It's also used by the Router for things like generating link addresses and, in your case, mapping the path supplied to requestAction() to a controller. Behind the scenes, Cake is creating a URL string based on your parameters, and then passes it off to the Router to find the correct controller. Since no such route exists, it fails.
As a solution, I would actually recommend not using a controller for that logic. Depending on what it does, a component or a helper may be a better place.
Look at line 156. You commented out the line that loads CakePHP's default routes.
Related
I am trying to use Laravel 5.2's RESTful resource controllers. However, when moving from my index to create, I would like to pass a parameter as the create page should be partially filled in.
edit
The form that will be 'created' will have populated fields from the database already. So the create should take the id from the user that is clicked in the index.
My temporary solution:
Route::get('consultation/{id}', 'ConsultationController#create');
Route::resource('consultation', 'ConsultationController', ['except' => ['create']]);
Is there a way to add this to an options array in the same line as resource?
Thanks
Edit: I suppose in this case, my store would also need the same {id} parameter.
I was also curious about that but I think that the Laravel Documentation is pretty clear about this:
If it becomes necessary to add additional routes to a resource
controller beyond the default resource routes, you should define those
routes before your call to Route::resource
And they add the following, meaning for me that if you want to override a defined route, you just need to put the definition on top of the Route::resource() definition.
otherwise, the routes defined by the resource method may
unintentionally take precedence over your supplemental routes
EDIT
After better understanding the question, I would let the restful controller as is, and create a new route like /user/{user-id}/consultations/create that is much more "restful" like.
Currently learning Symfony. Just wondering about route names, which the book doesn't really seem to say in advance what they are for.
As I understand it, #Route annotations are given a default name based on the bundle, controller and action name. So what's the point in specifying your own route names?
Is it good practice to either leave all routes to have a default name?
Is it better practice to specify your own name for every route?
What if two routes have the same name?
How am I expected to know if I'm using a route name which is already taken, perhaps in another bundle?
Route names are important because your code will often need to build url's. The route name is how you specify which url to construct. Names are also handy for listeners (such as authentication) which process specific url's.
Annotation generated default names are fine but tend to be long and could change on you.
Yes, I would give every route a custom name for convenience and readability. Plus, I don't use annotations. To me at least, storing routes in a central file make the code easier to maintain. It avoids the need to search through multiple controller files trying to determine which code handles which request.
Routes with the same name will replace any previously loaded route. It's useful if you wish to override a route from a third party bundle.
Namespace your route by using some form of your bundle name as a prefix.
I'm coming From CodeIgniter to Laravel.
So, is a bad idea using automatic routes to all of controllers?
Route::controller(Controller::detect());
Should I use this instead creating routes in routes.php?
Yes this is bad.
Controller::detect() is actually not present in Laravel 4 because it is a bit broken.
detect() will go through your filesystem and return controller files, but this is a bad idea because the order you define your routes matters. If you have any nested controllers you will find this breaking very easily.
detect() will also return files in a different order depending on the file system, so this leads to a lot of unpredictability.
I would argue that you should define all your routes any ways, it is a lot easier to read and debug.
One of interesting things about Laravel that CI does not have is that for certain pages, you can route directly to the view without needing a controller at all. Think about static pages like 'About Us'. CodeIgniter would need you to set up a controller + view for that, even though the controller will do barely anything. In case of Laravel, you can route directly to a view in this case.
Setting up routes manually will allow you to set these short-circuited routes.
Automatic detection is a bad idea.
You can use routes or use Route::controller('mycontroller') or and array of controllers like Route::controller(array('mycontroller', mycontroller2');
Then you get the benefit, without the autodetect.
in laravel 4 :
you can use Restful Controller like documentation http://laravel.com/docs/controllers#restful-controllers
But
Route::controller() must take two parameters as minimum requirement
first parameter stands for URL respond to ,and second parameter is name of controller
also
you can write third parameter into Route::controller() is an array with names of actions (name of action with HTTP verb )and routes names for this actions
ex:
Route::controller('users','UsersController',array(
'getUsers' =>"listUsers" ,
));
route name for getUsers action is listUsers
Below is a good example to follow for CRUD and general purpose routing
type php arisan controller:make SampleController
edit routes.php and add
Route::resource('sample', 'SampleController');
Then type
php artisan routes to show the newly created routes
I'm working on a Zend Framework API and need to follow a particular format for URLs, so I was hoping for some help regarding how to configure the routing correctly.
http://example.com/module/controller/method/actionNameHere
The above URL would need to route to the function actionNameHereAction.
Any help is appreciated.
The beauty of routing is that it gives you the tools for hiding exactly those pieces of information you are putting into your URL.
Apart from that, as far as I know ZF routes by default so that your URL would end up in ...
the action with name method of
the controller with name controller of
the module with name module
So either your example-URL is complicating things or you are almost there.
B/c actionNameHere would be a paramter you could handle in your action with name "methodAction".
But I think you want your URL to look like:
example.com/module/controller/actionNameHere
In order to produce the URLs needed, I ended up creating a custom Dispatcher, as it wasn't in the routing that URLs were being converted from actionNameHere to actionnamehereAction, but in the dispatcher. I extended the standard dispatcher and overrode this behaviour so that the action name in the URL remained case-sensitive.
How can I configure Zend_Controller_Router to use the default route by default? For whatever reason unknown, it uses the current route.
This is causing some annoying routing issues, and I don't feel like editing hundreds of links to explicitly use the default route is a reasonable solution to add one custom route.
Your question isn't obvious and is confusing, I will try though.
I am going to assume you do not mean Zend_Controller_Route but in fact are talking about links generated in your views.
You can use baseUrl() like this
About Us
You can set the baseUrl in your Bootstrap like this
Zend_Controller_Front::getInstance()->setBaseUrl("/");
If you are working locally you may want to set it to something like this
Zend_Controller_Front::getInstance()->setBaseUrl("/my-app/public");
Using baseUrl() in your views will now use that path to base all your URLs on. Remember you must wrap your links in baseUrl() for this to work.
The url() helper is not useful for this, this is used for doing things like pagination where you want the current parameters of the request object to be maintained.
Hope that helps.
From Matthew Weier O'Phinney:
The logic you quote above is within
the assemble() method. assemble() may
be called with or without routing
having occurred. By default, the
assumption is that if no route name is
provided, the currently matched route
is utilized -- and if matching was
never performed (or no route matched),
the 'default' route will be used.
The rationale for using the currently
matched route is that typically you
will be routing within the same schema
-- as an example, if you are within a RESTful paradigm, the majority of your
links will be within the current
resource. If we were to follow your
suggestion to utilize the "default"
route when no route is specified (at
least, that's how I interpret what you
said), because the Rest_Route is not
the default route, every single call
to assemble() would require using the
route name -- which others would
deride as causing "headaches."
Personally, I prefer to be explicit
and always specify the route name;
this way I know exactly what route I'm
targetting during assemble(), which
makes debugging easier.
Sounds like the only solution is to set the route for each link to use 'default' instead of leaving it NULL.