Laravel 路由版本 文件夹

Laravel 路由版本 文件夹

本文作者LockieDeng,PHP工程师,致力于倒腾各种前沿技术,欢迎交流:

dengmuuming@163.com

0、目的

1、路由定义

app/Providers/RouteServiceProvider.php

  • map()方法新增自己的路由方法,如:$this->mapChatV1();

image-20210903110457733

  • 源码
<?php
    
/**
* 文件路径 app/Providers/RouteServiceProvider.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();

        // 聊天接口V1版本
        $this->mapChatV1();
        //
    }

    /**
     * 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 "chat" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapChatV1()
    {
        Route::prefix('chat/v1')
//            ->middleware('chat')
            ->namespace($this->namespace)
            ->group(base_path('routes/chat/v1.php'));
    }
}

  • 实现mapChatV1()方法

image-20210903110517086

2、路由实现

routes/chat/v1.php

  • 创建文件routes/chat/v1.php
  • 写入路由

image-20210903142408581

  • 源码
<?php
    
/**
* 文件路径 routes/chat/v1.php
**/
    
use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/


//Route::get('depapier', '\App\Http\Controllers\Chat\V1\Test\Depapier@show');  // 全路径写法
Route::get('depapier', 'Test\Depapier@show'); // 简单写法

3、控制器

app/Http/Controllers/Chat/V1/Test/Depapier.php

image-20210903111644709

  • 源码
<?php

/**
* 文件路径 app/Http/Controllers/Chat/V1/Test/Depapier.php
**/
    
namespace App\Http\Controllers\Chat\V1\Test;

use App\Http\Controllers\Controller;

class Depapier extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        dd(123123);
        die();
    }

    public function show()
    {
        dd(321);
        die();
    }
}

4、路由访问

http://laraveltest.com/chat/v1/depapier

image-20210903111657905

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LockieDeng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值