Pregunta

I am trying to route multiple urls to the same controller function using part of those urls as parameters for a method within the controller. This is based on the routing functionality of the Laravel Framework, i'm not asking for general help in regards htaccess/pretty urls etc. Very specifically i'd like to know how to achieve this within the Laravel routes.php file.

The URLs I am trying to route are:

domain.com/articles

domain.com/downloads

domain.com/videos

domain.com/digests

Now I know I can do something like this in my routes.php:

Route::get(array('digests','articles','videos','downloads'), 'content@list_content');

And this will route to this function in my content controller:

public function action_list_content(){}

However i'd actually like the function to look like this:

public function action_list_content($type){}

And whatever is in the first part of the url (i.e. digests, articles, videos or downloads) should be sent as the $type parameter to the action_list_content method.

Is this possible?

I should note that i'd rather not use (:any) since it would be really greedy and accept "domain.com/anyrandomtext"

¿Fue útil?

Solución

Laravel converts wildcards into regular expressions before they are processed further. So, in effect, you can just jump straight into it by supplying a regular expression:

Route::get('(articles|downloads|videos|digests)', 'content@list_content');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top