Blog/How to organize your Laravel app with Website and Admin Panel
ยท2 min readยท
Paulo CastellanoPaulo Castellano

How to organize your Laravel app with Website and Admin Panel

See how organize your Laravel application in two parts, public website and admin panel. Separate your controllers, views and route files!

LaravelSoftware DevelopmentTech
How to organize your Laravel app with Website and Admin Panel

Many projects have different parts in their structure, like a blog for example.

You have the administrative part, where for example you will manage your posts, newsletters, schedule new posts and etc.

However their administrative panel and the public part where their readers access are totally different, they depend on middleware, repositories, controllers, filters, cache and totally different views.

To have a better organization of these standards, I created this organization structure that works for my daily life, but feel free to adapt the structure to your needs.

Throughout this article we will use the structure of a blog as an example.

Separating views

The first thing I do is create โ€œaliasโ€ for the views โ€œviews/adminโ€ and โ€œviews/blogโ€ as in the example below:

This way you will be able to use these โ€œaliasesโ€ when necessary, as in the example below, where โ€œblog::path_to_viewโ€ is equal to โ€œviews/blog/path_to_viewโ€.

Namespaces structure

Another thing I always do and it helps me a lot in the organization is to separate the controllers in different namespaces, like โ€œControllers/Adminโ€ and โ€œControllers/Blogโ€.

With this structure, your project is organized and its code is isolated in the respective namespaces.

Bonus:

If you want to be even more organized, you can still break your route file in two.

Instead of just keeping the file โ€œroutes/web.phpโ€ you can divide it into โ€œroutes/admin.phpโ€ and โ€œroutes/blog.phpโ€.

To do this, create the two files โ€œroutes/admin.phpโ€ and โ€œroutes/blog.phpโ€, notice that Iโ€™m already putting the correct namespaces and prefixes in the route group as well.

To stop reading just the โ€œroutes/web.phpโ€ file and start reading the โ€œroutes/admin.phpโ€ and โ€œroutes/blog.phpโ€ files, you need to adjust your RouteServiceProvider as follows.

Okay, now your project will be much more organized and easy to maintain.

If you have any suggestions for improvement or criticism of this structure, leave it in the comments below to discuss.

Important:

  • You will need to run: composer dump-autoload for the changes to take effect.
  • This organization tutorial works from Laravel 5.0 to 7.x

Share this article