Friday, 15 February 2013

php - RESTful trailing slash routes in laravel -



php - RESTful trailing slash routes in laravel -

i have next routes in laravel 3 restful api project

route::delete('/(:any)', 'resources@destroy'); route::delete('users/(:any)', 'users@destroy');

the problem having when user sends delete request /users/

what want happen users@destroy route called parameter null. in controller have exception user trying delete null resource.

what seems happening resource@destroy route called parameter users. has undesired impact of deleting users resource.

i know modify .htaccess technically /users/ belong users controller not resources controller. want maintain relationship.

i wondering if there simple way solve within laravel?

edit: have above working reply below. have error in routes

route::get('users/(:any?)', 'users@show'); route::get('users', 'users@index');

/users , /users/ both phone call users@index don't want.

i need /users go users@index , /users/ go users@show null parameter

one point need consider.

routes defined without front-slash. exception default route represented front-slash.

so route::delete('/(:any)', 'resources@destroy') cause undesired result.

moreover, order wrong.

(:any) match user , send request resources controller.

so need is,

change order (make reverse). change route of resources considering according rules. resources/delete etc.....

what want happen users@destroy route called parameter null. in controller have exception user trying delete null resource.

to this, (after making changes above....)

change route of user/(:any) user/(:any?) create 2nd segment optional.

after that, straight forward.

$foo = uri::segment(2, null); //rest follow. edit

now, next code,

route::get('users/(:any?)', 'users@show'); route::get('users', 'users@index');

does not create sense.

if type user, router suppose take?

user@show no optional segment or user@index?

routes design removes ambiguity. going in opposite direction making ambiguity.

just create simple route.

like show

user/show

delete

user/delete

edit

user/edit

etc....

the type of routing applying confusing both users , developers carries ambiguity.

php rest laravel

No comments:

Post a Comment