[英]Load url from routes for a controller
As CodeIgniter comes out of the box, it supportes one controller for each unique url. 当CodeIgniter开箱即用时,它为每个唯一的URL支持一个控制器。 So, let's say i have the following urls in my routes.php file: 因此,假设我的routes.php文件中包含以下URL:
$route['default_controller'] = "homepage";
$route['404_override'] = "homepage/not_found";
$route['^products$'] = "product/list";
$route['^product/(:any)$'] = "product/details";
My urls will look like this: 我的网址看起来像这样:
In the controller where I build the products list, I create the url for each product like this: 在构建产品列表的控制器中,我为每个产品创建网址,如下所示:
$this->db->from('products');
$products_result = $this->db->get();
$data['products'] = array();
foreach ($products_result->result() as $row)
{
$data['products'][] = array(
'title' => $row->title,
'image' => $row->image,
'url' => site_url('product/' . $row->url)
);
}
$this->load->view('products/list_view', $data);
But there is too much redundancy for each url. 但是每个URL都有太多冗余。 I have to write the url every time I want to echo it somewhere. 每当我想在某处回显该URL时,都必须编写该URL。 If I want to change the url, I have to open each php file and replace them all. 如果要更改URL,则必须打开每个php文件并全部替换。
Question: Isn't there a "method" I can use to call the controller name and its method and that "method" returns the url for that one? 问题:没有可用于调用控制器名称及其方法的“方法”,并且该“方法”返回该控制器的URL吗? Something like this: 像这样:
build_site_url('product/details', array('my-product-url'));
where the first argument is the controller and its method (since only one controller exists for a url pattern), and the second argument is the array of url parts. 其中第一个参数是控制器及其方法(因为url模式仅存在一个控制器),第二个参数是url部分的数组。
What you're looking for is a feature called reverse routing . 您正在寻找的是一种称为反向路由的功能。 There is no such feature built into CodeIgniter at this time, and any library I could find was done in like 2010, so probably not the most up-to-date. 目前,CodeIgniter尚未内置此类功能,我可以找到的任何库都是在2010年完成的,因此可能不是最新的。
However, there are articles and pull requests out there relating to reverse routing, so if you are experienced enough, you should be able to put something together for your application. 但是,这里有一些文章和有关反向路由的请求请求,因此,如果您有足够的经验,则应该可以为您的应用程序添加一些东西。
Otherwise, your best bet may be to create helpers for your most common URLs (like your products). 否则,最好的选择是为最常用的URL(例如产品)创建帮助程序。 So you could do something like echo product_url('my-product-name');
因此,您可以执行类似echo product_url('my-product-name');
, and you would only need to adjust the URL in the helper function. ,而您只需要在辅助函数中调整URL。
I am not sure what exactly that you desired, but you can override controller behaviour using _remap()
function; 我不确定您到底想要什么,但是您可以使用_remap()
函数覆盖控制器的行为。
look: http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping 外观: http : //ellislab.com/codeigniter/user-guide/general/controllers.html#remapping
eg 例如
public function _remap($method){
if ($method == 'product/details') {
///do what you want to do
}
You could either use a default lookup function in the product controller (described in the CI Routing docs ; look at the examples). 您可以在产品控制器中使用默认的查找功能(在CI Routing文档中进行了介绍 ;请参见示例)。
$route['product/(:any)'] = "catalog/product_lookup";
A URL with "product" as the first segment, and anything in the second will be remapped to the "catalog" class and the "product_lookup" method. URL以“ product”作为第一段,第二段中的任何内容都将重新映射到“ catalog”类和“ product_lookup”方法。
Or you could use database driven routes. 或者,您可以使用数据库驱动的路由。
Add the table product_slugs to your MySQL database: 将表product_slugs添加到您的MySQL数据库:
CREATE TABLE IF NOT EXISTS `product_slugs` (
`id` bigint(20) NOT NULL auto_increment,
`slug` varchar(192) collate utf8_unicode_ci NOT NULL
PRIMARY KEY (`id`),
KEY `slug` (`slug`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
Replace the code in application/config/routes.php with the one below: 用下面的代码替换application / config / routes.php中的代码:
$route[ 'default_controller' ] = 'main';
$route[ '404_override' ] = 'error404';
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'product_slugs' );
$result = $query->result();
foreach( $result as $row )
{
$route[ $row->slug ] = 'product/detail/$1;
}
All you would have to do then is to create a record when you create a product entry and you're done: 然后,您要做的就是在创建产品条目并完成操作时创建一条记录:
INSERT INTO `product_slugs` (`slug`) VALUES ('name-of-the-product');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.