在一些开源的电商系统中,我们会看到这样的url:
http://www.intosmile.com/retro-deep-v-neck-irregular-tassels-hem-sleeveless-dress.html
对于这种 retro-deep-v-neck-irregular-tassels-hem-sleeveless-dress.html
肯定不是对应的真实文件,而是一个url的自定义
1.首先我们需要建立一个表 url_rewrite,用来做对应关系
retro-deep-v-neck-irregular-tassels-hem-sleeveless-dress.html 对应到 category/product/index sku='xxxxxx'
当访问这个url的时候,我们通过表的查询就找到了这些信息,当然,这些信息是产品数据保存的时候,写入了 表:url_rewrite
然后就可以生成把数据取出来
2.在yii2 的代码修改
新建一个类:
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace common\web;
use frontend\models\core\Store;
use frontend\models\core\Config;
class Request extends \yii\web\Request
重写 request的getRewriteUri方法
##重写url ,根据得到的url,在 $rewrite_url_arr 中查找是否存在 ,如果存在,
##说明这个url是自定义的url,需要更改成真实的url, 更改RequestUri为原来的url
protected function getRewriteUri($requestUri){
$front_requestUri = "";
$wh_requestUri = "";
$dh_requestUri = "";
if(strstr($requestUri,"?")){
$arr = explode("?",$requestUri);
$front_requestUri = $arr[0];
$wh_requestUri = $arr[1];
}else if(strstr($requestUri,"#")){
$arr = explode("#",$requestUri);
$front_requestUri = $arr[0];
$dh_requestUri = $arr[1];
}else{
$front_requestUri = $requestUri;
}
//echo $requestUri;exit;
if($custom_uri = $this->getCustomUrl($front_requestUri)){
if($wh_requestUri){
return $custom_uri."?".$wh_requestUri;
}else if($dh_requestUri){
return $custom_uri."#".$dh_requestUri;
}else{
return $custom_uri;
}
}else{
return $requestUri;
}
}
#根据当前的自定义uri,得到数据库保存的真实uri。
protected function getCustomUrl($uri){
# 去掉头部的/
if(substr($uri,0,1) == "/"){
$uri = substr($uri,1);
}
$url_rewrite_coll = \Yii::$app->mongodb->getCollection('url_rewrite');
$one = $url_rewrite_coll->findOneConvert(['request_path' => $uri]);
if($one['_id']){
$type = $one['type'];
$type_data_id = $one['type_data_id'];
Global $page_handle;
if($type == 'product'){
$product_coll = \Yii::$app->mongodb->getCollection("catalog_product");
$where = ["_id"=>(int)$type_data_id,"status"=>1 ];
if(!Config::param("out_stock_product_is_show")){
$where['is_in_stock'] = 1;
}
$product_data = $product_coll->findOneConvert($where);
if($product_data['_id']){
$page_handle = $product_data;
return '/catalog/product/index';
}
}else if($type == 'category'){
$category_table = "catalog_category";
$category_coll = \Yii::$app->mongodb->getCollection($category_table);
$category_data = $category_coll->findOneConvert(["_id"=>(int)$type_data_id,"status"=>1 ]);
if($category_data['_id']){
$page_handle = $category_data;
return '/catalog/category/index';
}
}else if($type == 'cmspage'){
$cmspage_coll = \Yii::$app->mongodb->getCollection("cms_page");
$cmspage_data = $cmspage_coll->findOneConvert(["_id"=>(int)$type_data_id,"status"=>1 ]);
if($cmspage_data['_id']){
$page_handle = $cmspage_data;
return '/home/index/page';
}
# 下一步做。blog没有做。
}else if($type == 'blog'){
$blog_coll = \Yii::$app->mongodb->getCollection("blog_article");
$blog_data = $blog_coll->findOneConvert(["_id"=>(int)$type_data_id,"status"=>1 ]);
if($blog_data['_id']){
$page_handle = $blog_data;
return '/blog/blog/index';
}
}else if($type == 'blogcategory'){
$blogcategory_coll = \Yii::$app->mongodb->getCollection("blog_category");
$blogcategory_data = $blogcategory_coll->findOneConvert(["_id"=>(int)$type_data_id ]);
if($blogcategory_data['_id']){
$page_handle = $blogcategory_data;
return '/blog/category/index';
}
}else{
return false;
}
}else{
$rewrite_url_arr = array(
'contacts'=>'customer/contacts/index',
);
if($rewrite_url_arr[$uri]){
return "/".$rewrite_url_arr[$uri];
}
return false;
}
}
'request' => [
'class' => 'common\web\Request',
'enableCookieValidation' => true,
'enableCsrfValidation' => true,
'cookieValidationKey' => '432-Md23444432432973432423421423@#44@',
'noCsrfRoutes' => [
'catalog/product/addreview',
'favorite/product/remark',
'paypal/ipn/index',
'paypal/ipn',
'catalog/api/product',
'catalog/api/productdelete',
'catalog/api/category',
'catalog/api/categorynew',
'catalog/api/productnew',
'customer/contacts/save'
//'',
//'',
],
],
重写了request 就可以实现 这种自定义url了。