[英]How to move my controllers into a separate package folder in Laravel 5.2?
I build a small app using laravel 5.2. 我使用laravel 5.2构建了一个小应用程序。 I put all of my files into a folder called Surveys located at
App/Modules/Surveys
. 我将所有文件放入位于
App/Modules/Surveys
名为Surveys的文件夹中。 "No worries, I am planning to move the Modules out of the App folder, I just need to get the controller to work" “不用担心,我打算将模块移出App文件夹,我只需要让控制器工作”
Currently my controller is located on App/Http/Controllers/SurveysController
目前我的控制器位于
App/Http/Controllers/SurveysController
I want to move my controller folder so it is located on App/Modules/Surveys/Controllers/Frontend/SurveysController
我想移动我的控制器文件夹,使其位于
App/Modules/Surveys/Controllers/Frontend/SurveysController
How would I tell laravel to resolve the controller out of this path App/Modules/Surveys/Controllers/Frontend/
instead of App/Http/Controllers
? 我如何告诉laravel解决控制器的路径
App/Modules/Surveys/Controllers/Frontend/
而不是App/Http/Controllers
?
I tried to do this into a service provider but it is still not working 我试图将此做到服务提供商,但它仍然无法正常工作
<?php
namespace App\Modules\Surveys\Providers;
use Illuminate\Support\ServiceProvider;
class PackageServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if (! $this->app->routesAreCached()) {
require __DIR__.'/../routes.php';
}
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->make('Modules\Surveys\Controllers\Frontend\SurveyController');
}
}
What should I do to make the routes point to the correct controller? 我该怎么做才能使路线指向正确的控制器?
Just for clarity my Application instructor starts at the App 为了清楚起见,我的应用指导员从App开始
App/Https/Controllers App/Modules/Surveys
I think I figured it out. 我想我明白了。
Inside the App/Providers/RouteServiceProvider
file, I changed the value of $namespace
variable to an empty string like this 在
App/Providers/RouteServiceProvider
文件中,我将$namespace
变量的值更改为这样的空字符串
Changed the following line protected $namespace = 'App\\Http\\Controllers';
更改了以下行
protected $namespace = 'App\\Http\\Controllers';
To this protected $namespace = '';
对于这个
protected $namespace = '';
Then inside my routes.php file I used the namespace like this 然后在我的routes.php文件中,我使用了这样的命名空间
Route::group([
'namespace' => 'Modules\Vendname\Surveys\Controllers\Frontend',
], function()
{
Route::get('/survey', ['as' =>'survey.index', 'uses' => 'SurveysController@index']);
});
I am not sure why Taylor have the value hard coded in the service provider and not an option in the configs but this solution seems to work! 我不确定为什么Taylor在服务提供商中具有硬编码的价值,而不是配置中的选项,但这个解决方案似乎有效!
I would been better if there is a dynamic way to override that value. 如果有一种动态方式来覆盖该值,我会更好。
By default controller's namespace is App\\Http\\Controllers which is defined in RouteServiceProvider and has a property $namespace so all controllers are supposed to be inside the App\\Http\\Controllers controller. 默认情况下,控制器的命名空间是App \\ Http \\ Controllers,它在RouteServiceProvider中定义并具有属性$ namespace,因此所有控制器都应该位于App \\ Http \\ Controllers控制器内。
I think you have more modules and not only Surveys module, you can set the RouteServiceProvider's $namespace property to 'App\\Modules' and define your routes as: 我认为你有更多的模块而不仅仅是Surveys模块,你可以将RouteServiceProvider的$ namespace属性设置为'App \\ Modules'并将你的路由定义为:
Route::get('some/route', ['uses' => 'Surveys\Controllers\Frontend\SurveyController@someMethod']);
If you feel that a huge prefix namespace on each route is ugly you could create a ModuleFooRouteServiceProvider for each module and require a file like 'routesModuleFoo.php' the same way as RouteServiceProvicer does and register those service providers on config/app.php file like: 如果你觉得每条路径上的一个巨大的前缀命名空间是丑陋的,你可以为每个模块创建一个ModuleFooRouteServiceProvider,并且需要一个像“routesModuleFoo.php”这样的文件,就像RouteServiceProvicer一样,并在config / app.php文件中注册那些服务提供者:
'providers' => [
...
App\Modules\Surveys\SurveysRouteServiceProvider::class,
App\Modules\ModuleFoo\ModuleFooRouteServiceProvider::class,
...
]
And ModuleFooServiceProvider would look like: ModuleFooServiceProvider看起来像:
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
class ModuleFooServiceProvider extends ServiceProvider
{
protected $namespace = 'App/Modules/ModuleFoo/Controllers';
public function boot(Router $router)
{
parent::boot($router);
}
public function map(Router $router)
{
$router->group(['namespace' => $this->namespace], function ($router) {
require app_path('Http/routesModuleFoo.php');
});
}
}
And your routes for that module will be defined on routesModuleFoo.php 您将在routesModuleFoo.php上定义该模块的路由
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.