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

Ajholloway's avatar

How to organize blade views, components, and livewire

My application has several different roles for users - each of them being mutually exclusive. Student, Teacher, Principal, and IT Admin. The roles are all mutually exclusive and have different dashboards, CRUD workflows, etc. They operate on the same data but will be doing entirely different things with it and therefore need to have different views with different controls. Because of this, I've organized my resources into the following structure:

/views/[role]/[feature]

Ex:

/views/student/grades/
/views/student/courses/
/views/student/teachers/
/views/student/school/
/views/teacher/courses/
/views/teacher/school/
/views/principal/school/
etc...

The shared components live in /views/components. Any components that are exclusive to a role will go in their role's directory:

/views/components/
/views/[role]/components

Ex:

/views/student/components/
/views/teacher/components/
/views/principal/components/

Then I add livewire into the mix. So now I've got:

/views/livewire/[role]/components
/views/livewire/[role]/[feature]

Ex:

/views/components/
/views/student/components/
/views/livewire/student/components/
/views/livewire/student/grades/
/views/livewire/student/courses/
/views/livewire/student/teachers/
/views/teacher/components/
/views/teacher/courses/
/views/teacher/school/
/views/livewire/teacher/components/
/views/livewire/teacher/courses/
/views/livewire/teacher/school/
/views/principal/components/
/views/principal/schools/
/views/principal/livewire/components
/views/livewire/principal/school/
etc...

The number of directories is growing exponentially and I'm constantly forgetting where certain bits of code live. It's getting out of hand...

How do you recommend I keep these organized when I'm dealing with livewire, components, and multiple roles?

0 likes
4 replies
LaryAI's avatar
Level 58

Organizing Blade views, components, and Livewire components in a Laravel application with multiple user roles can indeed become complex. Here are some strategies to help you manage this complexity more effectively:

1. Use Namespaces for Livewire Components

Instead of placing Livewire components in deeply nested directories, you can use namespaces to organize them. This way, you can keep your directory structure flatter and more manageable.

For example, you can define your Livewire components like this:

namespace App\Http\Livewire\Student;

use Livewire\Component;

class Grades extends Component
{
    public function render()
    {
        return view('livewire.student.grades');
    }
}

And in your Blade view, you can reference it like this:

@livewire('student.grades')

2. Group Views by Feature Instead of Role

Instead of organizing views strictly by role, consider organizing them by feature and then by role. This can help reduce the number of directories and make it easier to find related views.

For example:

/views/grades/student.blade.php
/views/grades/teacher.blade.php
/views/courses/student.blade.php
/views/courses/teacher.blade.php
/views/school/student.blade.php
/views/school/teacher.blade.php
/views/school/principal.blade.php

3. Use Blade Components for Shared UI Elements

If you have UI elements that are shared across different roles, use Blade components to encapsulate them. This way, you can reuse the same component in different views without duplicating code.

For example, create a Blade component for a shared table:

// app/View/Components/SharedTable.php
namespace App\View\Components;

use Illuminate\View\Component;

class SharedTable extends Component
{
    public function render()
    {
        return view('components.shared-table');
    }
}

And use it in your Blade views:

<x-shared-table :data="$data" />

4. Use View Composers for Shared Data

If you need to share data across multiple views, consider using view composers. This can help you keep your controllers clean and reduce the amount of duplicated code.

For example:

// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\View;
use App\View\Composers\SharedDataComposer;

public function boot()
{
    View::composer(['grades.*', 'courses.*'], SharedDataComposer::class);
}

And in your composer:

// app/View/Composers/SharedDataComposer.php
namespace App\View\Composers;

use Illuminate\View\View;

class SharedDataComposer
{
    public function compose(View $view)
    {
        $view->with('sharedData', 'This is shared data');
    }
}

5. Document Your Structure

Finally, document your directory structure and naming conventions. This can be as simple as a README file in your resources/views directory that explains where different types of views and components should go. This will help you and your team remember where things are and why they are organized in a certain way.

Example Directory Structure

Here’s an example of how you might organize your views and components:

/resources/views
    /components
        /shared-table.blade.php
    /grades
        /student.blade.php
        /teacher.blade.php
    /courses
        /student.blade.php
        /teacher.blade.php
    /school
        /student.blade.php
        /teacher.blade.php
        /principal.blade.php
    /livewire
        /student
            /grades.blade.php
            /courses.blade.php
        /teacher
            /courses.blade.php
            /school.blade.php
        /principal
            /school.blade.php

By following these strategies, you can keep your Blade views, components, and Livewire components organized and maintainable, even as your application grows.

1 like
vincent15000's avatar

Just a question : for what purpose are you using blade components ? You seem to have components for students, other components for teachers, ...

I'm used to create blade components for visual things, for example a button, so that when I display the button component anywhere in the application, it will always have the same design.

Ajholloway's avatar

@vincent15000 Good question - the reason for this is that the teacher and the student may have entirely different designs. Different color themes, different button shapes, etc. For some of the larger components, certain controls and features might not be visible to a student while they are visible to a teacher.

1 like
vincent15000's avatar

@Ajholloway Ok, it makes sense.

Unfortunately you have a big project and you have a large amount of views and components.

I think that there is no real solution : organizing the views and components by role or by feature will result in the same complexity.

But you have another solution : why not creating modules ? You could have the Teacher module, the Student module and the Principal module. This way you could have the views files in each module's place instead of having all views together at the same place.

In this series, you have a great example on how you can organize your application in modules.

https://laracasts.com/series/modular-laravel

Please or to participate in this conversation.