Y
Y
Yuri Smith2018-04-17 14:27:45
Laravel
Yuri Smith, 2018-04-17 14:27:45

How to make one level of nesting handled by different controllers in Laravel 5?

Hello.
There is a site on Laravel 5, now different types of pages with different levels of nesting are implemented there.
For example, articles are available at site.ru/category/cat-name/article-name/ (category is a static word)
There are other post types that are available at site.ru/cat-name/cat-sub-name/ article-name/
There are several more types of records, where after the site name there is a static word, we look at it and in the routes which controller to connect. Each record type has its own table in the database.

Route::get('/category/{category}/{article_slug}/', ['as' => 'article', 'uses' => '[email protected]']);

Now they say you need to remove nesting and that all articles be at the first level of nesting, i.e. available at site.ru/article-name/ and so all entries from all types.
How can this be implemented?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yan-s, 2018-04-17
@Yan-s

For what reason does anyone care about the level of nesting in the url? How does this someone suggest resolving the situation, matching identifiers in the url?
If the task gets rid of nesting (slashes), then prefixes can be used, for example:
Route::get('/article_{article_slug}/', ['as' => 'article', 'uses' => '[email protected] ']);
Route::get('/category_{category}/', ['as' => 'category', 'uses' => '...']);

K
Konstantin B., 2018-04-17
@Kostik_1993

Create a urls table or whatever you want to call it, it will have id, slug, sluggable_type, sluggable_id fields.
If you could notice, we will use polymorphic relationships
Create one controller that will process all requests, receive a string by Slug, then based on the type you get the desired model, here you will most likely have to write a service that will process the result and play the role of a universal controller that will receive the model, and then, depending on the type, call the desired view

I
iljaGolubev, 2018-04-17
@iljaGolubev

And what's the problem then? If all articles are in the same table, then:

// GET site.ru/seo_friendly_article_name
Route::get('/{article_slug}', '[email protected]');

// в контроллере
public function article(Article $article_slug){
    // определяете нужное представление
     return view("articles.categories.{$article_slug->category_id}.show", ['article' => $article_slug]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question