kimbarcelona01's avatar

Laravel redirect to custom page instead of an article using Slug

Hi, I'm trying to find a solution online but, no luck. Basically, what i want to achieve is this.

http://localhost:8000/sample-post

It should redirect to the sample-post article as usual. I created a PostController@show for this. (no problem around this part)

What I want is for the page to use another controller for custom pages like below.

http://localhost:8000/about

http://localhost:8000/terms

http://localhost:8000/more-custom-pages

I want these to redirect to different controller. I actually don't want to create different controllers for each but just one controller to manage these custom pages. More custom pages can be define in my admin side, for example i can create another page called http://localhost:8000/partners so I don't want to go to web.php and add another route there for a purpose of a new custom page.

How can I let Laravel use a different controller for specific set of custom pages that I can define in Admin side? Both uses URL/{SLUG} approach

0 likes
3 replies
aurawindsurfing's avatar

Two ways I can think of this is:

  1. Encapsulate the route groups in middleware that at the end calls specific controller? But this is probably not the right way to do this.

  2. Make SlugController that groups the slugs and redirects to the correct controller?

There is probably a better way to do this tough.

Hope it helps!

arukomp's avatar
arukomp
Best Answer
Level 10

Looks like you just need one catch-all route and one controller to handle them all. The slugs should then be shared between custom pages and the blog posts/articles. An easy way to achieve that would be to hold them in a single table as well.

But how to distinguish between custom pages and blog posts? You could abstract the common settings into a Page model (such as id, slug, some meta data maybe), and then extend those into other types, such as Article, or a CustomPage.

The way I see it is through polymorphic relationships.

Tables:

"pages":
  - id
  - slug
  - title
  - pageable_id
  - pageable_type
  - (timestamps)

"articles":
  - id
  - content
  ...

"custom_pages":
  - id
  - content
  ...

Models:

class Page extends Model
{
    public function pageable()
    {
        return $this->morphTo();
    }
}

class Article extends Model
{
    public function page()
    {
        return $this->morphOne('App\Page', 'pageable');
    }
}

class CustomPage extends Model
{
    public function page()
    {
        return $this->morphOne('App\Page', 'pageable');
    }
}

Controller:

class HomeController extends Controller
{
    public function show($slug)
    {
        $page = \App\Page::whereSlug($slug)->first();

        $page->pageable;    // <--- that would be your Article, or a CustomPage, depending on the `pageable_type` field in the `pages` table
    }
}

Read more about polymorphic relationships - https://laravel.com/docs/5.7/eloquent-relationships#polymorphic-relationships

Please or to participate in this conversation.