Laravel 5将视图和模型保存在资源视图目录中的单独文件夹中 [英] Laravel 5 keep views and models in separate folder in resources views directory
问题描述
我已经大量搜索了我的要求,但没有找到适合我的解决方案.我正在使用laravel 5创建一个Web应用程序,在这里我想将与管理员访问相关的所有控制器都保留在controllers文件夹的admin子文件夹中.为此,我在 Laravel控制器子文件夹路由中找到了很好的答案,但是现在我担心的是保持视图和模型放在单独的文件夹中,因此可以轻松管理它们.例如laravel 5允许我们以以下格式保留控制器的所有视图
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
- 资源
- 意见
- 布局
- controllername(用于控制器特定的视图文件夹)
- index.blade.php
- resources
- views
- layouts
- controllername(for controller specific view folder)
- index.blade.php
现在我想要这样的东西
- 资源
- 意见
- 布局
- 管理员
- controllername(用于控制器特定的视图文件夹)
- index.blade.php
- resources
- views
- layouts
- admin
- controllername(for controller specific view folder)
- index.blade.php
我还在寻找与laravel 5直接在App文件夹中提供模型的模型相同的结构.我在网上的某个地方发现可以直接在App中为模型创建文件夹,而您只需要在模型中指定名称空间,例如下面的App Directory中的admin文件夹代码.
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 } ?>
任何帮助都可以使我进一步从事mu项目.
Any help will allow me to go further for mu project.
推荐答案
您对应用程序的结构拥有完全的自由.如果您的观点是
You have complete freedom on the structure of your application. If your view is
resources/views/admin/mycontroller/index.blade.php
并将您的控制器放置在
app/Http/Controllers/Admin/MyController.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'); } }
关于模型,Laravel还是灵活的,并且使用PSR-4自动加载功能,因此名称空间结构必须与目录结构匹配.如果要将
Users
模型放置在App\Models\Admin
命名空间中,则只需创建以下文件夹结构: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 theApp\Models\Admin
namespace, then simply create this folder structure:app/ Models/ Admin/ Users.php ... resources/ vendor/
在
Users.php
文件中,放入模型类:In the
Users.php
file, put your model class:<?php namespace App\Models\Admin; use Illuminate\Database\Eloquent\Model; class Users extends Model { // ... }
请注意,
Users
的名称空间与目录结构匹配:Note that the namespace of
Users
matches the directory structure:-
App
映射到app/
目录 -
App\Models
映射到app/Models
-
App\Models\Admin
映射到app/Models/Admin
- 最后将
App\Models\Admin\Users
映射到app/Models/Admin/Users.php
App
is mapped to theapp/
directoryApp\Models
is mapped toapp/Models
App\Models\Admin
is mapped toapp/Models/Admin
- and finally
App\Models\Admin\Users
is mapped toapp/Models/Admin/Users.php
如果
Users
类旨在替换标准的LaravelUser
Eloquent模型,那么您还必须更改config/auth.php
配置文件并替换行If your
Users
class is intended to replace the standard LaravelUser
Eloquent model, then you also have to change theconfig/auth.php
config file and replace the line'model' => 'App\User',
使用
'model' => 'App\Models\Admin\Users',
我希望这会有所帮助.
这篇关于Laravel 5将视图和模型保存在资源视图目录中的单独文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- controllername(for controller specific view folder)
- views
- controllername(用于控制器特定的视图文件夹)
- 意见
- views
- 意见