在laravel項(xiàng)目開(kāi)發(fā)的過(guò)程中,為了能更好的展現(xiàn)PC端和移動(dòng)端不同內(nèi)容之間的差異,我們通常會(huì)使用www.test.com訪問(wèn)PC端的內(nèi)容,而m.test.com去訪問(wèn)移動(dòng)端的內(nèi)容,這種架構(gòu)的設(shè)計(jì)方式,能更好的符合搜索引擎的規(guī)則,特別是針對(duì)百度移動(dòng)適配規(guī)則能有更好的表現(xiàn)。
百度移動(dòng)適配規(guī)則如下:
1. 為提升搜索用戶在百度移動(dòng)搜索的檢索體驗(yàn),會(huì)給對(duì)應(yīng)PC頁(yè)面的手機(jī)頁(yè)面在搜索結(jié)果處有更多的展現(xiàn)機(jī)會(huì),需要站點(diǎn)向百度提交主體內(nèi)容相同的PC頁(yè)面與移動(dòng)頁(yè)面的對(duì)應(yīng)關(guān)系,即為移動(dòng)適配。為此,百度移動(dòng)搜索提供“移動(dòng)適配”服務(wù),如果您同時(shí)擁有PC站和手機(jī)站,且二者能夠在內(nèi)容上對(duì)應(yīng),即主體內(nèi)容完全相同,您可以通過(guò)移動(dòng)適配工具進(jìn)行對(duì)應(yīng)關(guān)系提交。
2. 自適應(yīng)站點(diǎn)不需要使用移動(dòng)適配工具。 

通過(guò)規(guī)則適配,使用正則表達(dá)式描述PC-移動(dòng)URL關(guān)系,適用于網(wǎng)站大多數(shù)目錄頁(yè)
那么在Laravel中如何去實(shí)現(xiàn),詳情如下:
1、在app\http\Controllers文件夾里面創(chuàng)建PC端和移動(dòng)端不同的控制器文件夾,分別對(duì)應(yīng)PC端的路由文件及移動(dòng)端路由文件

2、修改app\http\providers\RouteServiceProvider.php文件
<?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';
    protected $homeNamespace = 'App\Http\Controllers\Web';//PC端
    protected $mNamespace = 'App\Http\Controllers\M';//移動(dòng)端
    /**
     * 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()
    {
        // $this->mapApiRoutes();
        // $this->mapWebRoutes();
        // 通過(guò)判斷$_SERVER['HTTP_HOST']的入口來(lái)區(qū)分www和m
        $sld_prefix = explode('.',$_SERVER['HTTP_HOST'])[0];
        if('www' == $sld_prefix){
            $this->mapHomeRoutes();
        }elseif('m' == $sld_prefix){
            $this->mapMRoutes();
        }
    }
    /**
     * PC端指定路由文件
     */
    protected function mapHomeRoutes()
    {
        Route::middleware('web')
            ->namespace($this->homeNamespace)
            ->group(base_path('routes/www.php'));
    }
    /**
     * 移動(dòng)端指定路由文件
     */
    protected function mapMRoutes()
    {
        Route::middleware('web')
            ->namespace($this->mNamespace)
            ->group(base_path('routes/m.php'));
    }
    /**
     * 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'));
    }
}
3、在routes目錄下創(chuàng)建www.php路由
Route::get('/', 'IndexController@index'); //PC端網(wǎng)站首頁(yè)
4、在routes目錄下創(chuàng)建m.php路由
Route::get('/', 'IndexController@index'); //移動(dòng)首頁(yè)

5、測(cè)試效果


現(xiàn)在我們已經(jīng)實(shí)現(xiàn)不同域名之間的訪問(wèn)效果。



 鄂公網(wǎng)安備 42090202000212號(hào)
鄂公網(wǎng)安備 42090202000212號(hào)