1. 云栖社区>
  2. PHP教程>
  3. 正文

Moving Controllers to sub-folders in a correct way - Laravel Daily

作者:用户 来源:互联网 时间:2017-12-01 13:29:54

Moving Controllers to sub-folders in a correct way - Laravel Daily - 摘要: 本文讲的是Moving Controllers to sub-folders in a correct way - Laravel Daily, If your application gets bigger, it makes sense to structure Controllers with sub-folders. But it takes a little more e

If your application gets bigger, it makes sense to structure Controllers with sub-folders. But it takes a little more effort than just moving the files here and there. Let me explain the structure.

For example, we want to have a sub-folder app/Http/Controllers/Adminand then inside of it we have our AdminController.php, that’s fine. What we need to do inside of the file itself:

1. Correct namespace– specify the inner folder:

PHP

namespace App/Http/Controllers/Admin;

2. Use Controller– from your inner-namespace Laravel won’t “understand” extends Controller, so you need to add this:

PHP

use App/Http/Controllers/Controller;

3. Routes – specify full path

This wouldn’t work anymore:

PHP

Route::get('admin', '[email protected]');

This is the correct way:

PHP

Route::get('admin', 'Admin/[email protected]');

And that’s it – now you can use your controller from sub-folder.

Facebook Twitter Google+ LinkedIn

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

现在注册,免费体验40+云产品,及域名优惠!

云服务器9.9元/月,大学必备
Kubernetes上基于Istio体验云原生应用实践 资料下载