FLOWERS开发日志(二)域名与路由

FLOWERS系统采用laravel框架开发。环境使用HOMESTEAD。我们三个小伙伴分工协作。
考虑到将来系统的扩展,采用了多二级域名的形式:

  • 用户中心:passport.flowers.test
  • 主站:flowers.test
  • 后台:flowers.test/admin (使用laravel-admin开发)
  • 社区: bbs.flowers.test
  • 课堂: class.flowers.test

采用多域名同入口的模式,解决多项目要共享公用代码的问题:

Route::domain('flower.test')->group(function () {
    Route::get('/','PagesController@index')->name('root');
});

Route::domain('passport.flower.test')->group(function () {
    //Route::get('/','ask\HomeController@index');
    // 用户身份验证相关的路由
    Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
    Route::post('login', 'Auth\LoginController@login');
    Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// 用户注册相关路由
    Route::get('register', 'Auth\RegisterController@showRegistrationForm')->name('register');
    Route::post('register', 'Auth\RegisterController@register');

// 密码重置相关路由
    Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.request');
    Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
    Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
    Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.update');

// Email 认证相关路由
    Route::get('email/verify', 'Auth\VerificationController@show')->name('verification.notice');
    Route::get('email/verify/{id}/{hash}', 'Auth\VerificationController@verify')->name('verification.verify');
    Route::post('email/resend', 'Auth\VerificationController@resend')->name('verification.resend');
});

用户系统在laravel自带的Auth脚手架的基础上开发。
后台在laravel-admin的基础上开发。
前端工作流采用laravel-mix,计划应用bootstrap+vue等前端框架。

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

建议把路由文件按域名分开,RouteServiceProvider中注册一下,更加方便管理,切分工明确

3年前 评论
ruanwenwu (楼主) 3年前

多谢指导,已经采用了您说的RouteSerivceProvider的方法,区分了路由文件

<?php

namespace App\Providers;

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

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';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * 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()
    {
        //获取访问地址前缀
        $sld_prefix = explode('.',$_SERVER['HTTP_HOST'])[0];

        switch ($sld_prefix) {
            case 'www':
                $this->mapWebRoutes();
                break;
            case 'passport':
                $this->mapPassportRoutes();
                break;
            case 'api':
                $this->mapApiRoutes();
                break;
            default:
                $this->mapWebRoutes();
                break;
        }
    }

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


    protected function mapPassportRoutes(){
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/passport.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'));
    }
}
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!