4

I am trying to have a REST design, but I am running in a bit of a problem. I have a resource schedule. Therefore, the normal notation of /schedules/{id} is not very applicable since I would like to have /schedules/{day}/{month}/{year} and then apply REST, and have /edit and such.

Is there a way to do this with Route::resource() ? or do I need to do them through Route::get() ?

3 Answers 3

4

As far as I know route::resource only gives you the routes that are detailed in the documentation so for what you want you would need to declare your own route. It is still restful and if it is only one of the resourceful routes you want to change you should still be able to do the following because the routes are prioritized in the order they are declared.

Route::get('schedule/{day}/{month}/{year}/edit', array('as' => 'editSchedule', 'uses' => 'ScheduleController@edit'));
Route::resource('schedule', 'ScheduleController');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. So if I were to extend this for all of the restful actions, I need to define each one through the code above right?
Yes but change get to whatever verb you want to use for the action
That's wrong. Laravel's resource can get multiple parameters.
3

Yes, there is a very simple way. Here is an example:

Specify your route like this:

Route::resource("schedules/day.month.year", "ScheduleController");

The request will be like this:

/schedules/day/1/month/12/year/2014

And now you can get all three parameters in show method of your contoller:

public function show($day, $month, $year)

2 Comments

this doesn't seem to work in 5.1. Can you please provide source for your answer?
This solution only works with literal strings, not with route model binding.
2

Hi there this might be handy if you want to call your route by name. Also you can use one or multiple parameters. It works with me on laravel 5.1

According to the laravel docs: http://laravel.com/docs/5.1/routing#named-routes

Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
    //
}]);

$url = route('profile', ['id' => 1]);

This works with Route:resource aswell.

for example:

Route::resource('{foo}/{bar}/dashboard', 'YourController');

Will create named routes like: {foo}.{bar}.dashboard.show

To call this with the route method, you set it up as followed.

route('{foo}.{bar}.dashboard.show', ['foo' => 1, 'bar'=> 2])

Which will create the url yourdomain.com/1/2/dashboard

Ill hope this is usefull.

Pascal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.