You'd want something like `Route::get('ticket/(value1}/{value2}', 'Controller@index'); The routing documentation will better explain what these values mean.
Route::resource()
defines a specific set of routes you can also read about in the routing docs, and then review by entering php artisan routes
in your shell in the app.
I've recently done something like this with a route such as:
Route::get('dosomething/{path?}', array('as' => 'dosomething', 'uses' => 'DosomethingController@show'))->where('path', '.+');
Where "dosomething" is your controller.
Then have something like below in your controller to split the url segments and make sense of them:
public function show($path)
{
$segments = explode('/', $path);
foreach($segments as $segment)
{
// do whatever you want with the segments
It works rather well in my project and handles any number of segments. Good luck.
EDIT: machuga's answer might be better suited to your use case if you have a defined set of parameters. In my situation I'm trying to handle an ever expanding amount of segments.
Thanks, sitesense. This seems to suit my purposes just fine.
Machuga, thank you for responding as well. Your method would have been cleaner, but I couldn't get it to work. I've gone over the routing documentation, but those examples keep logic in the router. I couldn't figure out how to translate that to my situation. I hope to work that out in the future, though.
A follow up to this question.
I don't see a way to capture the URL when there is no parameter present. At this point, I'm throwing an exception if there is no parameter in my request.
Route::get('ticket/{scan?}', array('as' => 'scan', 'uses' => 'TicketController@index'))->where('scan', '.+');
UPDATE: My problem was not giving the $scan variable a default value in the TicketController@index method.
In my case, I know the order that the url segments should appear in. It's an application that displays entities based on location so the url structure is like this:
/country/region/county/city/entity
I just count the url segments to know which segment holds the correct parameter like so:
foreach($segments as $segment)
{
$i++;
switch ($i) {
case 1:
$country = $segment;
break;
case 2:
$region = $segment;
break;
case 3:
$county = $segment;
break;
case 4:
$city = $segment;
break;
case 5:
$entity = $segment;
break;
}
No need for anything else if you know the order of the segments and what they relate to.
Have fun :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community