获取考虑 routes.php 的 CodeIgniter 链接
Obtain CodeIgniter links that consider routes.php
考虑到 routes.php,我如何才能在我的网站中 link 页?
示例:
$route['login'] = 'user/login';
上面的代码让我看到 "user/login" 只访问了 "login"。但是我怎样才能 link 使用内部路由 (user/login) 到那个页面并得到结果 "external route" "login".
我认为这很重要,因为我可以通过修改 "routes.php" 和 link 使用内部路由更改我的 URL。
从 Drupal 的角度来看,我可以使用内部路由 "node/1",而外部路由 url 可以是 "about-us"。所以如果我使用 "l('node/1')" 这将 return "about-us"。有没有类似"drupal_get_path_alias"的函数?
现在我无法在 CI 文档中找到任何指向正确方向的内容。
感谢您的帮助。
您可以使用 .htaccess
文件来做到这一点:
Redirect 301 /user/login http://www.example.com/login
你可以看看使用像
这样的东西
http://osvaldas.info/smart-database-driven-routing-in-codeigniter
这将允许您在数据库中配置路由。然后,如果您想通过这样的模型动态创建链接:
class AppRoutesModel extends CI_Model
{
public function getUrl($controller)
{
$this->db->select('slug');
$this->db->from('app_routes');
$this->db->where('controller', $controller);
$query = $this->db->result();
$data = $query->row();
$this->load->library('url');
return base_url($data->slug);
}
public function getController($slug)
{
$this->db->select('controller');
$this->db->from('app_routes');
$this->db->where('slug', $slug);
$query = $this->db->result();
$data = $query->row();
return $data->controller;
}
}
这些尚未经过全面测试,但希望能为您提供总体思路。
希望对您有所帮助:)
编辑-----------------------------
您可以创建一个 routes_helper.php 并添加一个类似
的函数
//application/helpers/routes_helper.php
function get_route($path)
{
require __DIR__ . '/../config/routes.php';
foreach ($route as $key => $controller) {
if ($path == $controller) {
return $key;
}
}
return false;
}
$this->load->helper('routes');
echo get_route('controller/method');
尽管此方法不支持可以添加以反映 :num 或 :any 通配符的 $1 $2 等变量,但这大致可以满足您的需求。您可以编辑该功能以添加该功能,但这将为您指明正确的方向:D
考虑到 routes.php,我如何才能在我的网站中 link 页?
示例:
$route['login'] = 'user/login';
上面的代码让我看到 "user/login" 只访问了 "login"。但是我怎样才能 link 使用内部路由 (user/login) 到那个页面并得到结果 "external route" "login".
我认为这很重要,因为我可以通过修改 "routes.php" 和 link 使用内部路由更改我的 URL。
从 Drupal 的角度来看,我可以使用内部路由 "node/1",而外部路由 url 可以是 "about-us"。所以如果我使用 "l('node/1')" 这将 return "about-us"。有没有类似"drupal_get_path_alias"的函数?
现在我无法在 CI 文档中找到任何指向正确方向的内容。
感谢您的帮助。
您可以使用 .htaccess
文件来做到这一点:
Redirect 301 /user/login http://www.example.com/login
你可以看看使用像
这样的东西http://osvaldas.info/smart-database-driven-routing-in-codeigniter
这将允许您在数据库中配置路由。然后,如果您想通过这样的模型动态创建链接:
class AppRoutesModel extends CI_Model
{
public function getUrl($controller)
{
$this->db->select('slug');
$this->db->from('app_routes');
$this->db->where('controller', $controller);
$query = $this->db->result();
$data = $query->row();
$this->load->library('url');
return base_url($data->slug);
}
public function getController($slug)
{
$this->db->select('controller');
$this->db->from('app_routes');
$this->db->where('slug', $slug);
$query = $this->db->result();
$data = $query->row();
return $data->controller;
}
}
这些尚未经过全面测试,但希望能为您提供总体思路。
希望对您有所帮助:)
编辑-----------------------------
您可以创建一个 routes_helper.php 并添加一个类似
的函数//application/helpers/routes_helper.php
function get_route($path)
{
require __DIR__ . '/../config/routes.php';
foreach ($route as $key => $controller) {
if ($path == $controller) {
return $key;
}
}
return false;
}
$this->load->helper('routes');
echo get_route('controller/method');
尽管此方法不支持可以添加以反映 :num 或 :any 通配符的 $1 $2 等变量,但这大致可以满足您的需求。您可以编辑该功能以添加该功能,但这将为您指明正确的方向:D