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

gdfsgdfgdfgdfg's avatar

PHP MVC understanding help

First, I want to say that it's the first time for me working with a PHP Framework and MVC and I have not found all the answers to my problems yet. I'm using Laravel 5.3 at the moment.

Problem: I have a website where a User can login and a few pages (I'm using Laravel Auth) and on a lot of pages I have a sidebar which is every time the same. Then I have on every page some content which is different (about, articles, ...). I heard that the most important thing is that you should never write code twice and there is the problem. On this sidebar there is a "service" which show content to the user but to show this I have an algorithm which needs the user data from the authenticated user and the Auth function is not available in Laravel 5.3 ServiceProvider or BaseController and was never meant to be.

My question now is how can I do that cleanly?

Now I serve the view in each, own controller and the content of this page (for example shoutbox, news) is also in that controller. (Not very clean in my opinion but didnt find any better way. Something I can improve here?).

I can serve the sidebar content on every Controller but this is not what I want. How can I do that? Am I using MVC right?

Thanks in advance!

0 likes
4 replies
paulredmond's avatar

I hope I understand correctly, it sounds like you are confused about how to show content in a sidebar to only logged in users?

If so, here is a good example in a blade template from the generated auth app layout:

https://github.com/laravel/framework/blob/5.3/src/Illuminate/Auth/Console/stubs/make/views/layouts/app.stub#L52-L75

I would make the sidebar part of the layout (or at least a partial template included in the layout with the sidebar code) if it will be used on every page.

You can also access user data in controllers with the request object:

request()->user()

Or if you Inject the request in your controller method:

public function show(\Illuminate\Http\Request $request)
{
    $user = $request->user();
    // ....
}

To answer this:

I heard that the most important thing is that you should never write code twice and there is the problem

Keeping code clean is important, but a little duplication won't hurt, don't obsess about DRY, try to keep it clean, test, refactor, and the codebase will improve over time. Maintainability can be more important than holding fast to principles no matter what. Including tests will help with maintainability.

Those are my opinions, but don't obsess over DRY or SRP to the point where you can't make the best decision in each case without paying tribute to the <insert pattern here> god.

1 like
gdfsgdfgdfgdfg's avatar

No I think I didnt write it clear enough.

I have a sidebar which is on every page the same and because of this I only want to write it ONCE in ONE controller but I dont know how I can do that because every Page has its own controller and I cant access for example the function sidebar from another controller - I have to write it in every Controller for every page.

I know there is a view_share command in Laravel for that (In Providers and BaseController) but the problem is that I have an algorithm which needs the auth user id and in Laravel 5.3 you cant access the auth in Providers or BaseController so I'm looking for a good way how I can do that. Hope you understand what I mean.

Thanks!

clay's avatar

Personally, I like view composers for always providing data to a view. May seem like overkill for one variable for one view, but they come in handy if your project grows. If you don't want to use full-blown class style composers, you can use a closure and Auth::user() is available within the closure.

// AppServiceProvider
<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    function boot(){
        View::composer('partials.sidebar', function($view){
            $user = Auth::user() ;
            $view->with('user', $user) ;
        });
    }
}

you'll need to be sure to check for an authorized user if you show the sidebar to unauthenticated users or you'll get an error for trying to call a method or property on null.

or create your own ServiceProvider and add it to config\app. Also, here is an alternative approach. I've never done this, but it should work as well.

1 like

Please or to participate in this conversation.