如何使用-分隔相关url ?
所以我有一个URL像"domain.de/thisismytest"但是我想有"domain.de/this-is-my-test" ?
目前我在我的路由。php
$route['this-is-a-test'] = "site/thisisatest";
在我非常简单的控制器中我有这样的代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_home");
$this->load->view("site_footer");
}
public function thisisatest() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_thisisatest");
$this->load->view("site_footer");
}
public function about() {
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about");
$this->load->view("site_footer");
}
}
您的配置似乎是正确的。这个路由配置行:
$route['this-is-a-test'] = "site/thisisatest";
映射到Site
类和thisisatest
方法。
你确定在配置中没有这个路由吗:
$route['thisisatest'] = "site/thisisatest";
?在这种情况下,两者将指向同一个方法。
无论如何,我认为问题是你引用的thisisatest
url在你的观点的某个地方。检查您是否没有调用site_url('thisisatest')
或base_url('thisisatest')
或domain.de/thisisatest
在视图的某个地方,并将其替换为site_url('this-is-a-test')
。
在您的索引函数中,您可以抓取url段并处理字符串:
$segment = $this->uri->segment(1); // the url segment you need
$segments = explode( '-', $segment );
$yourFunction = implode ( $segments );
检查函数是否存在
if(method_exists($this,$yourFunction))
$this->$yourFunction();
和在你的路由文件中指向你的索引:
$route['yourController/(:any)'] = 'yourController/index';