Laravel 5 keeps views and models in a separate folder in the resource directory
I searched for alot for my requirement, but I didn't find a working solution for me. I am creating a web application using laravel 5 where I want all the controllers related to admin access to go to the admin subfolder in the controllers folder. For this I found a good answer in Laravel Controller's subfolder routing , but now I'm worried about having to view views and models in separate folders so they can do without any problem. For example laravel 5 allows us to save all views for controllers in below format
- resources
- Views
- Layouts
- controller_name (for a specific controller view folder)
- index.blade.php
- Views
Now I want something like this
- resources
- Views
- Layouts
- admin
- controller_name (for a specific controller view folder)
- index.blade.php
- controller_name (for a specific controller view folder)
- Views
I am also looking at the same structure for the model as laravel 5, providing the model directly to the App folder. I found somewhere on the net that you can create a folder for the model in the application directly and you just need to specify the namespace in the model as shown below for the admin folder in the application directory.
<?php
namespace App\Models\Admin;
class Users extends Model{
// do some stuff here
}
?>
Any help will allow me to go further for the mu project.
source to share
You have complete freedom in the structure of your application. If your idea
resources/views/admin/mycontroller/index.blade.php
and your controller is put into a file app/Http/Controllers/Admin/MyController.php
, then you can use the view like this:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class MyController extends Controller {
public function index() {
return view('admin.controller.index');
}
}
As far as your models are concerned, again Laravel is flexible and uses PSR-4 autoloading, so your namespace structure must match your directory structure. If you want to place your model Users
in a namespace App\Models\Admin
, just create this folder structure:
app/ Models/ Admin/ Users.php ... resources/ vendor/
In the file, Users.php
enter the model class:
<?php
namespace App\Models\Admin;
use Illuminate\Database\Eloquent\Model;
class Users extends Model
{
// ...
}
Note that the namespace Users
follows the directory structure:
-
App
mapped to directoryapp/
-
App\Models
displayed onapp/Models
-
App\Models\Admin
displayed onapp/Models/Admin
- and finally
App\Models\Admin\Users
mapped toapp/Models/Admin/Users.php
If your class is Users
meant to replace the standard Laravel User
Eloquent model , then you also need to modify the config file config/auth.php
and replace the line
'model' => 'App\User',
from
'model' => 'App\Models\Admin\Users',
Hope this helped.
source to share