漂亮的url设置在codeigniter [英] pretty url setting in codeigniter
问题描述
我拥有控制器'package',函数'tour_package'和参数'1'的url。
http://www.mysite.in/package/tour_packages/1
这里的参数设置为id。我需要更改它与相应的url字符串
http://www.mysite.in/ package / tour_packages / American
I have the url with controller 'package',function 'tour_package' and parameter '1'. http://www.mysite.in/package/tour_packages/1 Here the parameter is set as the id. I need to change it with corresponding url string http://www.mysite.in/package/tour_packages/American
和另一个例子:
http://www.mysite.in/package/tour_packages/2
到
http://www.mysite.in/package/tour_packages/非洲
如何
推荐答案
首先我将更改url链接以使用 url_title url帮助器)。如果你使用ID的文本值创建查询,我假定这是一个数据库调用,这将给你一些像
(For example, if the record for id 1 is name="America");
(例如,如果id的记录1 is name =America);
echo 'http://www.mysite.in/package/tour-packages/'.url_title($name);
Would give;
会给予;
If you then add this extended router class it will automatically reformat dashes to underscores for you.
如果您添加此扩展路由器类,它会自动将破折号重新格式化为下划线。
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function set_class($class) {
$this->class = str_replace('-', '_', $class);
}
function set_method($method) {
$this->method = str_replace('-', '_', $method);
}
function _validate_request($segments) {
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT)) {
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0])) {
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0) {
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT)) {
show_404($this->fetch_directory().$segments[0]);
}
} else {
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT)) {
$this->directory = '';
return array();
}
}
return $segments;
}
// Can't find the requested controller...
show_404($segments[0]);
}
}
Then you can add an entry in the routes file (in config); something like;
然后您可以在routes文件中添加一个条目in config);
$route['package/tour_package/(:any)'] = "package/lookup_by_name/$1";
You then need to create a method in the package controller called lookup_by_name($name).
This method needs to do a sql query to your database where you get the id from the name value. You can then continue to load a view or do whatever you want.
然后,您需要在包控制器中创建一个名为 lookup_by_name / strong>。
此方法需要对您的数据库执行sql查询,您从名称值获取id。
E.g
public function lookup_by_id($name) {
// sql to get id from database record
// load the view?
$this->view($id);
}
public function view($id) {
// sql to load full record from id
$this->load->view('foo', $data);
}
这篇关于漂亮的url设置在codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!