Stefano Martini
Stefano Martini

Reputation: 455

Laravel mix static with dynamic slug in a base route

I have a question and I don't find a solution, I'm building a landing page section and in this section I will have some pages with identical layout, and content will be provider by db, some other pages will return a blade view.

How can I manager the route?

Url structure will be domani.com/landing/{slug}

Which is the best way to compare the {slug} with provider static slug and if not find search in the db?

Upvotes: 0

Views: 88

Answers (1)

Maik Lowrey
Maik Lowrey

Reputation: 17556

You can use in your route a where condition. Like that?

Route::get('/landing/{page}', App\Http\Controllers\LandingPageController::class)
    ->name('page')
    ->where('page', 'about-us|imprint|contact');

In this example only /landingpage/about-us, /landingpage/imprint, /landingpage/contact requestable.

And in your LandingPageController you can add a method with a switch case to redirect to the right page.

class LandingPageController {

    public function __invoke($page)
    {
        return view('pages.'.$page);
    }
}

Upvotes: 0

Related Questions