laravel如何自定义控制器目录

Posted 归一山人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel如何自定义控制器目录相关的知识,希望对你有一定的参考价值。

默认控制器在App\\Http\\Controllers目录下,如何自定义目录呢?

首先我们看一下laravel的请求周期

我们看一下服务提供者RouteServicePrivder文件中的一个函数


/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
  
     protected $namespace = \'App\\Http\\Controllers\';

 

1     protected function mapWebRoutes()
2     {
3         Route::middleware(\'web\')
4              ->namespace($this->namespace)
5              ->group(base_path(\'routes/web.php\'));
6     }

 

修改后

 

  

 1 <?php
 2 
 3 namespace App\\Providers;
 4 
 5 use Illuminate\\Support\\Facades\\Route;
 6 use Illuminate\\Foundation\\Support\\Providers\\RouteServiceProvider as ServiceProvider;
 7 
 8 class RouteServiceProvider extends ServiceProvider
 9 {
10     /**
11      * This namespace is applied to your controller routes.
12      *
13      * In addition, it is set as the URL generator\'s root namespace.
14      *
15      * @var string
16      */
17     protected $namespace = \'App\\Http\\Controllers\';
18     protected $platform = \'App\\Platform\';
19 
20     /**
21      * Define your route model bindings, pattern filters, etc.
22      *
23      * @return void
24      */
25     public function boot()
26     {
27         //
28         parent::boot();
29     }
30 
31     /**
32      * Define the routes for the application.
33      *
34      * @return void
35      */
36     public function map()
37     {
38         $this->mapApiRoutes();
39 
40         $this->mapWebRoutes();
41 
42         $this->mapPlatform();
43 
44         //
45     }
46 
47     /**
48      * Define the "web" routes for the application.
49      *
50      * These routes all receive session state, CSRF protection, etc.
51      *
52      * @return void
53      */
54     protected function mapWebRoutes()
55     {
56         Route::middleware(\'web\')
57              ->namespace($this->namespace)
58              ->group(base_path(\'routes/web.php\'));
59     }
60 
61     /**
62      * Define the "api" routes for the application.
63      *
64      * These routes are typically stateless.
65      *
66      * @return void
67      */
68     protected function mapApiRoutes()
69     {
70         Route::prefix(\'api\')
71              ->middleware(\'api\')
72              ->namespace($this->namespace)
73              ->group(base_path(\'routes/api.php\'));
74     }
75 
76     protected function mapPlatform()
77     {
78         Route::prefix(\'platform\')
79             ->middleware(\'web\')
80             ->namespace($this->platform)
81             ->group(base_path(\'app/Platform/routes.php\'));
82     }
83 }

app/platform/routers.php代码如下

 1 <?php
 2 
 3 use Illuminate\\Routing\\Router;
 4 
 5 //Platform::registerAuthRoutes();
 6 
 7 //Route::group([
 8 //    \'prefix\'        => config(\'platform.route.prefix\'),
 9 //    \'namespace\'     => config(\'platform.route.namespace\'),
10 //    \'middleware\'    => config(\'platform.route.middleware\'),
11 //], function (Router $router) {
12 //
13 //    return \'fff\';
14 //    $router->get(\'/\', \'HomeController@index\');
15 //
16 //});
17 Route::any(\'/\', \'Controllers\\HomeController@index\');

 

访问路由

 

以上是关于laravel如何自定义控制器目录的主要内容,如果未能解决你的问题,请参考以下文章

Laravel自定义消息

VSCode插件开发全攻略代码片段设置自定义欢迎页

从 Laravel 发送自定义 500 http 响应

如何在 Laravel 5.6 中向资源控制器添加自定义方法

如何在 laravel 中为资源控制器添加自定义函数?

如何将控制器中的 Laravel 4.2 beforeFilter 转换为 Laravel 5.2 中的自定义中间件