3

I have searched alot for my requirement but i did not found a working solution for me. I am creating a web application using laravel 5, where i want to keep all controllers which are related to admin access into admin sub-folder in controllers folder. For this i have found good answer on Laravel Controller Subfolder routing but now my concern is about keep views and models in separate folders so that they can be manage without any hassle. For example laravel 5 allow us to keep all views for controllers in below format

  • resources
    • views
      • layouts
      • controllername(for controller specific view folder)
        • index.blade.php

Now i want something like this

  • resources
    • views
      • layouts
      • admin
        • controllername(for controller specific view folder)
          • index.blade.php

I am also looking same kind structure for model to as laravel 5 provide model directly into App folder. I have found somewhere on net that you can create folder for model in App directly and you just need to specify namespace in model something like below code for admin folder in App Directory.

<?php

    namespace App\Models\Admin;

    class Users extends Model{
    // do some stuff here
    }

?>

Any help will allow me to go further for mu project.

Community
  • 1
  • 1
Manoj Sharma
  • 596
  • 1
  • 6
  • 23

1 Answers1

5

You have complete freedom on the structure of your application. If your view is

resources/views/admin/mycontroller/index.blade.php

and your controller is placed into the app/Http/Controllers/Admin/MyController.php file, then you can use the view this way:

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

class MyController extends Controller {
    public function index() {
        return view('admin.controller.index');
    }
}

Concerning your models, again Laravel is flexible and uses the PSR-4 autoloading, so your namespace structure must match the directory structure. If you want to place your Users model in the App\Models\Admin namespace, then simply create this folder structure:

app/
    Models/
        Admin/
            Users.php
...
resources/
vendor/

In the Users.php file, put your model class:

<?php
namespace App\Models\Admin;

use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    // ...
}

Note that the namespace of Users matches the directory structure:

  • App is mapped to the app/ directory
  • App\Models is mapped to app/Models
  • App\Models\Admin is mapped to app/Models/Admin
  • and finally App\Models\Admin\Users is mapped to app/Models/Admin/Users.php

If your Users class is intended to replace the standard Laravel User Eloquent model, then you also have to change the config/auth.php config file and replace the line

'model' => 'App\User',

with

'model' => 'App\Models\Admin\Users',

I hope this helped.

Marco Pallante
  • 3,923
  • 1
  • 21
  • 26
  • Great. Now what about if i want to use two different Users model separate for admin and normal user. should i place them in separate namespace and folder or we can utilize same model for both. – Manoj Sharma Jul 17 '15 at 16:05
  • That's not an easy thing to do. Try to search for that. The easiest way is to put an `isAdmin()` method in the model (and maybe some `scope...()` methods). – Marco Pallante Jul 17 '15 at 16:12
  • Thanks really helped me. – Louwki Apr 13 '16 at 10:41