• laravel如何自定义控制器目录


    默认控制器在AppHttpControllers目录下,如何自定义目录呢?

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

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

    
    
    /**
    * Define the "web" routes for the application.
    *
    * These routes all receive session state, CSRF protection, etc.
    *
    * @return void
    */
      
         protected $namespace = 'AppHttpControllers';
    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 AppProviders;
     4 
     5 use IlluminateSupportFacadesRoute;
     6 use IlluminateFoundationSupportProvidersRouteServiceProvider 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 = 'AppHttpControllers';
    18     protected $platform = 'AppPlatform';
    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 IlluminateRoutingRouter;
     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('/', 'ControllersHomeController@index');

    访问路由

  • 相关阅读:
    HTTP GET方法与POST方法有什么区别
    uniapp的pages.json之subPackages、preloadRule——分包
    爬虫与Python:(一)网络爬虫概念篇——7.Session和Cookie
    标签<view>文字自动换行
    CSS之flex布局
    爬虫与Python:(一)网络爬虫概念篇——6.HTTP基本原理
    uniapp 之使用分包——起源于微信错误码——800051
    爬虫与Python:(一)网络爬虫概念篇——3.爬虫的基本结构和工作流程
    爬虫与Python:(一)网络爬虫概念篇——4.爬虫策略
    水印解决方案专题
  • 原文地址:https://www.cnblogs.com/guiyishanren/p/10575549.html
Copyright © 2020-2023  润新知