Laravel 控制器子文件夹路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18850542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Controller Subfolder routing
提问by Tino
I'm new to Laravel. To try and keep my app organized I would like to put my controllers into subfolders of the controller folder.
我是 Laravel 的新手。为了尝试让我的应用程序井井有条,我想将我的控制器放入控制器文件夹的子文件夹中。
controllers\
---- folder1
---- folder2
I tried to route to a controller, but laravel doesn't find it.
我试图路由到一个控制器,但 Laravel 没有找到它。
Route::get('/product/dashboard', 'folder1.MakeDashboardController@showDashboard');
What am I doing wrong?
我究竟做错了什么?
回答by Ja22
For Laravel 5.3 above:
对于上面的 Laravel 5.3:
php artisan make:controller test/TestController
This will create the test
folder if it does not exist, then creates TestController
inside.
test
如果文件夹不存在,这将创建文件夹,然后TestController
在里面创建。
TestController
will look like this:
TestController
看起来像这样:
<?php
namespace App\Http\Controllers\test;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
public function getTest()
{
return "Yes";
}
}
You can then register your route this way:
然后,您可以通过以下方式注册您的路线:
Route::get('/test','test\TestController@getTest');
回答by Antonio Carlos Ribeiro
Add your controllers in your folders:
在您的文件夹中添加您的控制器:
controllers\
---- folder1
---- folder2
Create your route not specifying the folder:
创建不指定文件夹的路线:
Route::get('/product/dashboard', 'MakeDashboardController@showDashboard');
Run
跑
composer dump-autoload
And try again
然后再试一次
回答by user2014679
For those using Laravel 5 you need to set the namespace for the controller within the sub-directory (Laravel 5 is still in development and changes are happening daily)
对于使用 Laravel 5 的用户,您需要在子目录中为控制器设置命名空间(Laravel 5 仍在开发中,每天都在发生变化)
To get a folder structure like:
要获得如下文件夹结构:
Http
----Controllers
----Admin
PostsController.php
PostsController.php
namespace Admin\PostsController.php file like so:
命名空间 Admin\PostsController.php 文件如下:
<?php namespace App\Http\Controller\Admin;
use App\Http\Controllers\Controller;
class PostsController extends Controller {
//business logic here
}
Then your route for this is:
那么你的路线是:
$router->get('/', 'Admin\PostsController@index');
And lastly, don't for get to do either composer or artisan dump
最后,不要做作曲家或工匠转储
composer dump-autoload
or
或者
php artisan dump
回答by Ariful Haque
For ** Laravel 5 or Laravel 5.1 LTS both **, if you have multiple Controllers in Admin folder, Route::group
will be really helpful for you. For example:
对于 ** Laravel 5 或 Laravel 5.1 LTS **,如果您在 Admin 文件夹中有多个控制器,Route::group
将会对您非常有帮助。例如:
Update: Works with Laravel 5.4
更新:适用于 Laravel 5.4
My folder Structure:
我的文件夹结构:
Http
----Controllers
----Api
----V1
PostsApiController.php
CommentsApiController.php
PostsController.php
PostAPIController:
PostAPIController:
<?php namespace App\Http\Controllers\Api\V1;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostApiController extends Controller {
...
In My Route.php, I set namespace
group to Api\V1
and overall it looks like:
在 My Route.php 中,我将namespace
group设置为Api\V1
整体如下:
Route::group(
[
'namespace' => 'Api\V1',
'prefix' => 'v1',
], function(){
Route::get('posts', ['uses'=>'PostsApiController@index']);
Route::get('posts/{id}', ['uses'=>'PostssAPIController@show']);
});
For move details to create sub-folder visit this link.
有关创建子文件夹的移动详细信息,请访问此链接。
回答by Mervyn
1.create your subfolder just like followings:
1.创建您的子文件夹,如下所示:
app
----controllers
--------admin
--------home
2.configure your code in app/routes.php
2.在app/routes.php中配置你的代码
<?php
// index
Route::get('/', 'Home\HomeController@index');
// admin/test
Route::group(
array('prefix' => 'admin'),
function() {
Route::get('test', 'Admin\IndexController@index');
}
);
?>
3.write sth in app/controllers/admin/IndexController.php, eg:
3.在app/controllers/admin/IndexController.php中写入某处,例如:
<?php
namespace Admin;
class IndexController extends \BaseController {
public function index()
{
return "admin.home";
}
}
?>
4.access your site,eg:localhost/admin/test you'll see "admin.home" on the page
4.访问你的站点,例如:localhost/admin/test 你会在页面上看到“admin.home”
ps: Please ignore my poor English
ps:请忽略我拙劣的英文
回答by Tino
Just found a way how to do it:
刚刚找到了一种方法:
Just add the paths to the /app/start/global.php
只需将路径添加到 /app/start/global.php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/controllers/product',
app_path().'/models',
app_path().'/database/seeds',
));
回答by Dipen
php artisan make:controller admin/CategoryController
Here admin is sub directory under app/Http/Controllers and CategoryController is controller you want to create inside directory
这里 admin 是 app/Http/Controllers 下的子目录,CategoryController 是您要在目录中创建的控制器
回答by DevonDahon
In Laravel 5.6, assuming the name of your subfolder' is Api
:
在 Laravel 5.6 中,假设您的子文件夹的名称是Api
:
In your controller, you need these two lines:
在您的控制器中,您需要这两行:
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
And in your route file api.php
, you need:
在您的路线文件中api.php
,您需要:
Route::resource('/myapi', 'Api\MyController');
回答by Todor Todorov
I am using Laravel 4.2. Here how I do it:
I have a directory structure like this one:
app
--controllers
----admin
------AdminController.php
我正在使用 Laravel 4.2。我是怎么做的:
我有一个这样的目录结构:
app
--controllers
----admin
------AdminController.php
After I have created the controller I've put in the composer.json the path to the new admin directory:
创建控制器后,我在 composer.json 中放入了新管理目录的路径:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers/admin",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
Next I have run
接下来我跑了
composer dump-autoload
and then
进而
php artisan dump-autoload
Then in the routes.php I have the controller included like this:
然后在 routes.php 中,我包含了这样的控制器:
Route::controller('admin', 'AdminController');
And everything works fine.
一切正常。
回答by Koushik Das
If you're using Laravel 5.3 or above, there's no need to get into so much of complexity like other answers have said.
Just use default artisan command to generate a new controller.
For eg, if I want to create a User
controller in User
folder.
I would type
如果您使用的是 Laravel 5.3 或更高版本,则无需像其他答案所说的那样复杂。只需使用默认的 artisan 命令来生成一个新的控制器。例如,如果我想User
在User
文件夹中创建一个控制器。我会打字
php artisan make:controller User/User
In routes,
在路线中,
Route::get('/dashboard', 'User\User@dashboard');
doing just this would be fine and now on localhost/dashboard is where the page resides.
这样做就可以了,现在 localhost/dashboard 是页面所在的位置。
Hope this helps.
希望这可以帮助。