0712-2888027 189-8648-0214
微信公眾號(hào)

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號(hào)

當(dāng)前位置:主頁 > 技術(shù)支持 > PHP > laravel項(xiàng)目中www和m不同域名訪問路由配置

laravel項(xiàng)目中www和m不同域名訪問路由配置

時(shí)間:2024-10-22來源:風(fēng)信官網(wǎng) 點(diǎn)擊: 428次

在laravel項(xiàng)目開發(fā)的過程中,為了能更好的展現(xiàn)PC端和移動(dòng)端不同內(nèi)容之間的差異,我們通常會(huì)使用www.test.com訪問PC端的內(nèi)容,而m.test.com去訪問移動(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頁面的手機(jī)頁面在搜索結(jié)果處有更多的展現(xiàn)機(jī)會(huì),需要站點(diǎn)向百度提交主體內(nèi)容相同的PC頁面與移動(dòng)頁面的對(duì)應(yīng)關(guān)系,即為移動(dòng)適配。為此,百度移動(dòng)搜索提供“移動(dòng)適配”服務(wù),如果您同時(shí)擁有PC站和手機(jī)站,且二者能夠在內(nèi)容上對(duì)應(yīng),即主體內(nèi)容完全相同,您可以通過移動(dòng)適配工具進(jìn)行對(duì)應(yīng)關(guān)系提交。
2. 自適應(yīng)站點(diǎn)不需要使用移動(dòng)適配工具。 



通過規(guī)則適配,使用正則表達(dá)式描述PC-移動(dòng)URL關(guān)系,適用于網(wǎng)站大多數(shù)目錄頁

那么在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();

// 通過判斷$_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'));
}

/**
* 移動(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)站首頁

4、在routes目錄下創(chuàng)建m.php路由
Route::get('/', 'IndexController@index'); //移動(dòng)首頁



5、測(cè)試效果



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

欄目列表
推薦內(nèi)容
熱點(diǎn)內(nèi)容
展開