我试图重写目前正在使用的cakephp默认网址:
http://localhost/cakephpproject/property/view/1
进入此网址
http://localhost/cakephpproject/country/state/locality/id
首先,我必须从网址中删除property/view
。
到目前为止,我所做的是:
在 routes.php 文件中我有:
Router::connect(
'/:language/property/:country/:state_province/:locality/:id',
array(
'admin'=> false,
'user'=> false,
'language'=>null,
'controller' => 'properties',
'action' => 'view'
),
array(
'pass' => array('country', 'state_province', 'locality', 'id'),
'persist'=>array('language'),
'language' => '[a-z]{2}',
'id'=>'[0-9]+',
)
);
Router::connect(
'/property/:country/:state_province/:locality/:id',
array(
'admin'=> false,
'user'=> false,
'controller' => 'properties',
'action' => 'view'
),
array(
'pass' => array('country' , 'state_province' , 'locality' , 'id'),
'id' => '[0-9]+'
)
);
PS:我有两次,因为我有一个网站。
链接是这样的:
<?php
echo $this->Html->link(
__('View more'),
array(
'admin' => false,
'user' => false,
'controller' => 'properties',
'action' => 'view',
'country' => Inflector::slug($country,'-'),
'state_province' => Inflector::slug($state_province,'-'),
'locality'=> Inflector::slug($locality,'-'),
'id' => $id
),
array(
'escape' => false
)
);
?>
此链接生成的URL不是我想要的URL:
http://localhost/biriola/properties/view/country:United-Kingdom/state_province:unknown/locality:London/id:18
应生成的URL是:
http://localhost/biriola/United-Kingdom/unknown/London/18
我将生成的链接修改为:
<?php
echo $this->Html->link(
__('View more'),
array(
'admin' => false,
'user' => false,
'controller' => 'properties',
'action' => 'view',
Inflector::slug($country,'-'),
Inflector::slug($state_province,'-'),
Inflector::slug($locality,'-'),
$id
),
array(
'escape' => false
)
);
?>
这会生成以下网址:
http://localhost/biriola/properties/view/United-Kingdom/unknown/London/18
这是我想要的网址,但我必须从网址中删除properties/view/
,所以我不知道该怎么做。
我的确切CakePHP验证是: 2.5.6
这是完整的routes.php文件:
<?php
/**
* Routes configuration
*
* In this file, you set up routes to your controllers and their actions.
* Routes are very important mechanism that allows you to freely connect
* different URLs to chosen controllers and their actions (functions).
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Config
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* Here, we are connecting '/' (base path) to controller called 'Pages',
* its action called 'display', and we pass a param to select the view file
* to use (in this case, /app/View/Pages/home.ctp)...
*/
$languageCodes = array('language' => 'en|it|de|es|fr|ru|zh|pt');
Router::connect('/:language', array('controller' => 'properties', 'action' => 'index'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/', array('controller' => 'properties', 'action' => 'index'));
Router::connect('/:language/login', array('admin'=>false, 'user'=>false, 'controller' => 'users', 'action' => 'login'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/login', array('admin'=>false, 'user'=>false, 'controller' => 'users', 'action' => 'login'));
Router::connect('/:language/logout', array('admin'=>false, 'user'=>false, 'controller' => 'users', 'action' => 'logout'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/logout', array('admin'=>false, 'user'=>false, 'controller' => 'users', 'action' => 'logout'));
Router::connect('/:language/register', array('controller' => 'users', 'action' => 'add'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
Router::connect('/:language/faq', array('controller' => 'faqs', 'action' => 'index'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/faq', array('controller' => 'faqs', 'action' => 'index'));
Router::connect('/:language/faq/question', array('controller' => 'faqs', 'action' => 'add'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/faq/question', array('controller' => 'faqs', 'action' => 'add'));
Router::connect('/googlelogin', array('controller' => 'users', 'action' => 'googlelogin'));
Router::connect('/google_login', array('controller' => 'users', 'action' => 'google_login'));
Router::connect('/fb_login', array('controller' => 'users', 'action' => 'fb_login'));
Router::connect('/fblogin', array('controller' => 'users', 'action' => 'fblogin'));
/* paypal rout connect */
Router::connect('/:language/paymentOk', array('admin'=>false, 'user'=>false, 'controller' => 'properties', 'action' => 'paymentOk'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/paymentOk', array('admin'=>false, 'user'=>false, 'controller' => 'properties', 'action' => 'paymentOk'));
Router::connect('/:language/paymentEditOk', array('admin'=>false, 'user'=>false, 'controller' => 'properties', 'action' => 'paymentEditOk'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/paymentEditOk', array('admin'=>false, 'user'=>false, 'controller' => 'properties', 'action' => 'paymentEditOk'));
Router::connect('/:language/paymentCancel', array('admin'=>false, 'user'=>false, 'controller' => 'properties', 'action' => 'paymentCancel'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/paymentCancel', array('admin'=>false, 'user'=>false, 'controller' => 'properties', 'action' => 'paymentCancel'));
/*** Admin Routing URL-s */
Router::connect('/:language/admin', array('language'=>null,'admin' => true, 'controller' => 'users', 'action' => 'dashboard'),array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/admin', array('admin' => true, 'controller' => 'users', 'action' => 'dashboard'));
Router::connect('/:language/admin/:controller', array('language'=>null,'admin' => true, 'controller' => 'pages','action' => 'index', 'prefix' => 'admin'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/admin/:controller',array('admin' => true,'controller' => 'pages', 'action' => 'index', 'prefix' => 'admin'));
Router::connect('/:language/admin/:controller/:action/*', array('language'=>null,'admin' => true, 'controller' => 'pages','action' => null, 'prefix' => 'admin'),array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/admin/:controller/:action/*',array('admin' => true, 'controller' => 'pages','action' => null, 'prefix' => 'admin'));
/*** User Routing URL-s */
Router::connect('/:language/user', array('language'=>null,'user' => true, 'controller' => 'users', 'action' => 'dashboard', 'prefix' => 'user'),array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/user', array('user' => true, 'controller' => 'users', 'action' => 'dashboard','prefix' => 'user'));
Router::connect('/:language/user/:controller', array('language'=>null,'user' => true, 'controller' => 'pages','action' => 'index', 'prefix' => 'user'),array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/user/:controller', array('user' => true, 'controller' => 'pages','action' => 'index', 'prefix' => 'user'));
Router::connect('/:language/user/:controller/:action/*', array('language'=>null,'user' => true, 'controller' => 'pages','action' => null, 'prefix' => 'user'),array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/user/:controller/:action/*', array('user' => true, 'controller' => 'pages','action' => null, 'prefix' => 'user'));
/* sitemap */
Router::connect('/:language/sitemap.xml', array('language'=>null,'controller' => 'properties', 'action' => 'sitemap','admin'=>false, 'user'=>false), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/sitemap.xml', array('controller' => 'properties', 'action' => 'sitemap','admin'=>false, 'user'=>false));
/*** Languages */
Router::connect('/:language/:controller',array('language'=>null,'controller' => 'pages','action' => 'index'),array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/:controller',array('controller' => 'pages','action' => 'index'));
Router::connect('/:language/:controller/:action/*', array('language'=>null,'controller' => 'pages','action' => null), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/:controller/:action/*', array('controller' => 'pages','action' => null));
/* property view url rewriting */
Router::connect(
'/:language/:country/:state_province/:locality/:id',
array(
'admin'=> false,
'user'=> false,
'language'=>null,
'controller' => 'properties',
'action' => 'view'
),
array(
'pass' => array('country', 'state_province', 'locality', 'id'),
'persist'=>array('language'),
'language' => '[a-z]{2}',
'id'=>'[0-9]+',
)
);
Router::connect(
'/:country/:state_province/:locality/:id',
array(
'admin'=> false,
'user'=> false,
'controller' => 'properties',
'action' => 'view'
),
array(
'pass' => array('country' , 'state_province' , 'locality' , 'id'),
'id' => '[0-9]+'
)
);
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/:language/pages/*', array('language'=>null,'controller' => 'pages', 'action' => 'display'), array('language' => '[a-z]{2}','persist'=>array('language')));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
先谢谢
答案 0 :(得分:0)
在
Router::connect('/:controller/:action/*', array('controller' => 'pages','action' => null));
对于/abc/xyz/*
格式的所有网址,路由是全能的,并且由于订单在匹配/反向路由时很重要(从上到下),它会窃取您的反向路由尝试,从而吐出这样的网址具有命名(key:value
)元素,因为捕获路径没有定义任何这些参数。
因此,将新路线移到该路线之上,然后使用第一个link()
呼叫变量,这是正确的。
另请注意,这样的路由没有多大意义,除非URL中的控制器名称为{{{}},否则永远不会将/controller/action/xyz
之类的网址路由到Pages
控制器。 1}},对于反向路由,它只会匹配页面控制器,从而产生pages
之类的网址,从而使/pages/action/xyz
占位符的使用变得多余。
默认值是多余的并且令人困惑。
我建议您仔细研究路由文档,然后检查路由并删除冗余的默认值。