码迷,mamicode.com
首页 > 其他好文 > 详细

laravel如何自定义控制器目录

时间:2019-03-22 00:32:53      阅读:592      评论:0      收藏:0      [点我收藏+]

标签:control   code   ESS   group   cti   protect   facade   service   路由   

默认控制器在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如何自定义控制器目录

标签:control   code   ESS   group   cti   protect   facade   service   路由   

原文地址:https://www.cnblogs.com/guiyishanren/p/10575549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!