好小伙潇潇洒洒

小小的天,有大大的梦想,我有属于我的天!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

解决现在laraval目录的普遍单一问题,希望能给大家有所启发

此处以添加网站admin后台路由举例,步骤如下:

1. 在项目routes目录下添加路由文件admin.php;

2. 修改/app/providers/RouteServiceProvider.php

  (1)添加路由方法

protected function mapAdminRoutes()
{
    Route::prefix('admin')
         ->middleware('admin')
         ->namespace($this->namespace.'\Admin')
         ->group(base_path('routes/admin.php'));
}

  (2)将添加的路由方法加入map方法中执行

public function map()
{
    $this->mapApiRoutes();
    $this->mapWebRoutes();
    $this->mapAdminRoutes();    // 添加执行的路由方法
}

  附完整的RouteServiceProvider.php代码:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapAdminRoutes();
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

    /**
     * Define the "admin" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapAdminRoutes()
    {
        Route::prefix('admin')
             ->middleware('admin')
             ->namespace($this->namespace.'\Admin')
             ->group(base_path('routes/admin.php'));
    }
}

3. 在/app/Http/Kernel.php中添加home类名及其路径

protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        // 根据之前设置的路由规则名(admin)对应添加admin类名,并指向路由验证路径
        'admin' => \App\Http\Middleware\VerifyAdmin::class,
    ];

4. 在/app/Http/Middleware/文件夹下创建VerifyAdmin.php,并写入验证代码

代码如下:

 

<?php

namespace App\Http\Middleware;

use Closure;

class VerifyAdmin
{ 
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        return $next($request);
    }
}

5. 测试

    (1)在admin.php路由里添加一条路由规则,代码如下:

 

<?php
Route::get('test', 'IndexController@index');

  (2)创建控制器 Admin/IndexController,使用artisan命令创建一个资源控制器

php artisan make:controller Admin/IndexController --resource

6 开始访问 
http://XXXX/admin/test 
即可访问到Admin/IndexController类的index方法。 

注意:访问默认路由web.php下的规则不用加web,访问其他路由文件需要加上在RouteServiceProvider.php中定义的路由名。

posted on 2019-01-11 16:00  Mr.毛小毛  阅读(164)  评论(0编辑  收藏  举报