Multiple routes defaults to controller in Laravel -
i want able article short-link, articles/id, want there full link articles/id/category/slug. can not following work:
// route file: route::pattern('id', '[0-9]+'); route::pattern('cat', '^(?!create).*'); route::pattern('slug', '^(?!edit).*'); route::get('articles/{id}/{cat?}/{slug?}', ['as' => 'articles.show', 'uses' => 'articlescontroller@show']); // controller file: public function show($id, $cat = null, $slug = null) { dd('1: ' . $cat . ' | 2:' . $slug); } the following link articles/28/ullam/vel-repellendus-aut-est-est-esse-fugiat gives result:
string(53) "1: ullam/vel-repellendus-aut-est-est-esse-fugiat | 2:" i don't understand why it's not split, if remove ? in route definition works.
i have tried solution https://stackoverflow.com/a/21865488/3903565 , works, not when directed @ controller. why?
update; ended rearranging routes file:
route::pattern('id', '[0-9]+'); // articles route::get('articles/create', ['as' => 'articles.create', 'uses' => 'articlescontroller@create']); route::get('articles/edit/{id}', ['as' => 'articles.edit', 'uses' => 'articlescontroller@edit']); route::get('articles/{id}/{category?}/{slug?}', ['as' => 'articles.show', 'uses' => 'articlescontroller@show']); route::get('articles/{category?}', ['as' => 'articles.index', 'uses' => 'articlescontroller@index']); route::resource('articles', 'articlescontroller', ['only' => ['store', 'update', 'destroy']]);
the problem in lookahead patterns.
you need $ , class excluding / in order make work.
so here are:
route::pattern('id', '[0-9]+'); route::pattern('cat', '^(?!create$)[^/]*'); route::pattern('slug', '^(?!edit$)[^/]*'); route::get('articles/{id}/{cat?}/{slug?}', function($id, $cat, $slug) { dd($id.': ' . $cat . ' | 2:' . $slug); });
Comments
Post a Comment