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

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

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';//移動端
    /**
     * 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();
        // 通過判斷$_SERVER['HTTP_HOST']的入口來區(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'));
    }
    /**
     * 移動端指定路由文件
     */
    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)站首頁
4、在routes目錄下創(chuàng)建m.php路由
Route::get('/', 'IndexController@index'); //移動首頁

5、測試效果


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



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