Laravel5.5添加新路由文件并制定规则

Laravel5.5里面有4个默认的路由文件,其中web.php是默认路由文件,如果需要添加其他路由文件,按照以下步骤进行。

此处以添加网站home前端路由举例,我已经先在/app/Http/Controller/文件夹下创建了一个Home文件夹,这个文件夹下主要放网站前端控制器,其他步骤如下:

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

在这里插入图片描述
2. 修改/app/providers/RouteServiceProvider.php文件

  • 在文件最下方创建mapHomeRoutes方法
//
protected function mapPitcargoRoutes()
{
    Route::prefix('pitcargo')
         ->middleware('pitcargo')
         ->namespace($this->namespace.'\PitCargo')//这里的目录要和之后创建controllers下的目录一样
         ->group(base_path('routes/pitcargo.php'));
}
  • 将添加的路由方法加入map方法中执行
public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        $this->mapPitcargoRoutes(); //与刚才创建的方法名一样

        //
    }
附完整的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->mapPitcargoRoutes();

        //
    }

    /**
     * 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'));
    }

    //站点管理
    protected function mapPitcargoRoutes()
    {
        Route::prefix('pitcargo')
            ->middleware('pitcargo')
            ->namespace($this->namespace.'\PitCargo')
            ->group(base_path('routes/pitcargo.php'));
    }
}

  1. 在/app/Http/Kernel.php中添加home类名及其路径
protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

        // 根据之前设置的路由规则名(pitcargo)对应添加pitcargo类名,并指向路由验证路径
        'pitcargo' => \App\Http\Middleware\Pitcargo::class,
    ];
  1. 在/app/Http/Middleware/文件夹下创建Pitcargo.php,并写入验证代码
    在这里插入图片描述
    里面代码如下
<?php


namespace App\Http\Middleware;
use Closure;

class Pitcargo
{
    public function handle($request, Closure $next)
    {
        // if ("判断条件") {
        return $next($request);
        // }

        // 返回跳转到网站首页
        // return redirect('/');
    }
}

上面没有执行对pitcargo路由请求的验证,如果有需要自己加上。

  1. 在pitcargo.php路由里添加两条路由规则,代码如下:
<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 2020/1/17
 * Time: 11:35
 */

use Illuminate\Http\Request;

//登录
Route::get('login','LoginController@login');
  • 在/app/Http/Controller/PitCargo/文件夹下创建LoginController.php,创建方式可以直接在文件夹下创建文件,也可以使用工具匠( php artisan make:controller PitCargo\LoginController),控制器代码如下:
<?php

namespace App\Http\Controllers\PitCargo;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //
    public function login(){
        echo "515661";
    }
}

  1. 测试
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执念、很无奈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值