ozdemirburak / laravel-9-simple-cms

Laravel 9 content management system for starters.
MIT License
686 stars 302 forks source link

Working on routing with custom url #86

Closed ndollem closed 4 years ago

ndollem commented 4 years ago

Great work you have here...

I'd like to ask on how to create routing if I want to make a pretty url. Something like this

example.com/fashion/10-best-winter-coath

where "fashion" is the slug for categories and "10-best-winter-coath" is slug for article. Since your routing only take 1 paramater, I dont know how to do it using 2 parameters.

I try some combination like this

Route::get('{cSlug}/{aSlug}', ['as' => 'article', 'uses' => 'PageController@getArticle']); Route::get('c/{cSlug}/{aSlug}', ['as' => 'article', 'uses' => 'PageController@getArticle']);

But it didn't work

I'm new to laravel, so I hope you can help me how to work it out.

ozdemirburak commented 4 years ago

routes/web.php:

Route::get('c/{cSlug}/{aSlug}', ['as' => 'article', 'uses' => 'PageController@getArticle']);

app/Http/Controllers/Application/PageController.php:

public function getArticle(Category $category, Article $article)

app/Providers/RouteServiceProvider:

Route::bind('aSlug', function ($slug) {
    return Article::with('category')->where('slug', $slug)->firstOrFail();
});
Route::bind('cSlug', function ($slug) {
    return Category::with('articles')->where('slug', $slug)->firstOrFail();
});