Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jaytee's avatar
Level 39

Check the URL, If it matches, display a certain view

Hey guys,

So i'm working on a project where i'd like to have multiple dashboards (or rather, the same dashboard layout, but different URLs on the sidebar).

I thought, instead of creating a master page for each dashboard, use one master page and include the routes need if the certain check matches.

I had the following:

@if (Route::current()->getName() == 'admin:dashboard')
    @include('admin.templates.sidebar')
@endif

I also have a elseif to check for the second dashboard (that i've created so far) and all was good, but without thinking, as soon as the named route doesn't equal 'admin:dashboard', it doesn't display anything, so if i had: 'admin:dashboard:create', it wouldn't load.

What method would I use to check if the URL contains a specific (url) and if it has that domain, load that specific sidebar?

Such as, Load the general sidebar if the URL is:

/general

Or load the second sidebar if URL has/is

/second

Or is there an easier way to swap out sidebars ?

0 likes
5 replies
pmall's avatar

This is just a routing problem. Route all those urls to a different controller action loading the corresponding view, then if code is shared among those controller actions then put this code in a class/method/helper to avoid code duplication.

Route::get('dashboard1', 'DashboardController@dashboard1');
Route::get('dashboard2', 'DashboardController@dashboard2');
class DashboardController extends Controller
{
    public function dashboard1()
    {
        // shared code

        return view('dashboards.first');
    }

    public function dashboard2()
    {
        // shared code

        return view('dashboards.second');
    }
}
martinbean's avatar
Level 80

@DPJack Can you not use Blade sections for this? Have your dashboard layout, but then extending views that inject the necessary sidebar partial:

<!-- resources/views/layouts/dashboard.blade.php -->
<!DOCTYPE html>
<html>
  <body>
    <main>
      @yield('main')
    </main>
    <aside>
      @yield('sidebar')
    </aside>
  </body>
</html>
<!-- resources/views/dashboards/a.blade.php -->
@extends('layouts.dashboard')

@section('sidebar')
    @include('sidebars.a')
@endsection
<!-- resources/views/dashboards/b.blade.php -->
@extends('layouts.dashboard')

@section('sidebar')
    @include('sidebars.b')
@endsection
<!-- resources/views/dashboards/c.blade.php -->
@extends('layouts.dashboard')

@section('sidebar')
    @include('sidebars.c')
@endsection
2 likes
Jaytee's avatar
Level 39

@martinbean That's what I was thinking about, I was just trying to see if it loaded automagically without me needing to define which sidebar to use.

Jaytee's avatar
Level 39

@martinbean I've only just interpreted what you said :D

So have one main layout called dashboard,

then have multiple separate files that extend the main template, each including the different sidebar. Then if i need that one, extend my view off that sidebar.

Great, thanks dude.

martinbean's avatar

@DPJack Yup, exactly. Blade’s an inheritance template language, so it’s just playing to its strengths “layering” your templates like this.

Please or to participate in this conversation.