4
votes

I need help for the structure of my Laravel App.

What I want is basically this structure :

  • API
  • Admin panel
  • Public website

I started to build this folder structure which I think is pretty correct :

app/
   Http/Controllers/
       API/
       Back/
       Front/
resources/
   assets/
       js/
          back/
          front/
       sass/
          back/
          front/
   views/
       back/
       front/

Problems

Elixir: How can I avoid backend's libraries or assets to be included in frontend's code and vice versa after running gulp ?

Views: How to use namespaces in Laravel views ? For example, given pages1 in views/front/, which is extending some page2. I want Blade to automatically search page2 inside views/front directory when compiling (and not in views/back for example).

Routes: How to match routes with Controller's namespaces ? I know we can do

Route::group(['domain' => 'example.com'], function() {});

And also

Route::group(['namespace' => 'Admin'], function() {});

But how to combine them ?

1
Views and routes requirements are not hard to achieve, but I can not understand Exilir requirementmanix
Simply when I execute gulp command, my backend's and frontend's scripts and styles are compiled in 2 files : app.css and app.js. I don't want my backend's scripts available from front side... And so, what solution do you suggest for Views and Routes problems ?David
Oh, I see, let me show youmanix

1 Answers

4
votes

For Elixir, you can use Gulp module and then use mix variable to separate assets according to your needs. At this example, gulp will merge script.js and script2.js into a final file named public/backend/final.js.

// backend assets
elixir(function(mix){
    mix.scripts([
        'js/back/script.js',
        'js/back/script2.js',
    ], 'public/backend/final.js', 'resources/assets');

    // here you can declare another group of assets using 'mix'

});

For controllers namespaces, you edit app/Providers/RouteServiceProvider file and declare each namespace as:

public function map(Router $router)
{
    // backend namespace
    $router->group(['namespace' => 'App\Http\Controllers\BackEnd'], function ($router) {
        require app_path('Http/routes-backend.php');
    });

    // frontend namespace
    $router->group(['namespace' => 'App\Http\Controllers\FrontEnd'], function ($router) {
        require app_path('Http/routes-frontend.php');
    });

    // api namespace
    $router->group(['namespace' => 'App\Http\Controllers\API'], function ($router) {
        require app_path('Http/routes-api.php');
    });
}

Now, create three files:

  • app/Http/routes-backend.php > Routes for backend
  • app/Http/routes-frontend.php > Routes for frontend
  • app/Http/routes-api.php > Routes for API

For example, if you need to declare a new back end controller you detail it at app/Http/routes-backend.php:

Route::get('dashboard', 'DashboardController@show');

Finally for views:

How to use namespaces in Laravel views ? For example, pages1 in views/front/ blade-extending page2 will automatically search page2 inside view/front directory.

That is not possible, there is not way to page1 autoloads page2 without an explicit reference. Using @extends is enough, and personally I suggest this structure cause you can track your views with very more control.