Codeigniter 以这样的方式从 Mycontroller 访问 myFunction,其值为 myVarValue
http://my.webPage.com/index.php/MyController/myFunction/myVarValue
你想改变 URL,是的,你可以通过在 ../application/config/routes.php 上创建路由
$route['my-function-(:any)'] = 'MyController/myFunction/$1';
这是第一步
您创建了路线,但这些路线不起作用?
- 在文件 ../application/config/config.php 上设置 base_url,在您的情况下为 http://localhost/myapp,文件的第 26 行CI v3.1.6
- 设置默认控制器,任何进入你网站的人都会立即访问的默认控制器,你在../application/config/routes.php上设置这个,你的默认控制器可以是控制器的名称或控制器中的功能。
假设你有这个控制器
public class MyController extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
echo 'Hello people, im the index of this controller,'.
' im the function u will see everytime u '.
'access the route http://localhost/myApp/MyController';
}
public function notTheIndex(){
echo 'Hello, im the function u will see if u'.
'access http://localhost/MyController/notTheIndex';
}
}
假设这个控制器将是您的默认控制器,如 ci v3.16 上第 53 行的 routes.php 文件中所述 set
$route['default_controller'] = 'MyController';
//if u want to just access the index method
$route['default_controller'] = 'MyController/notTheIndex';
//if for some reason u have a method u would like to be executed
//by default as the index page
- 现在您希望 uri 保留您设置的名称
$route['some-beautiful-name'] = 'MyController/notTheIndex';
所以当你访问 http://localhost/some-beautiful-name 时,它会显示 MyController/notTheIndex 的结果
考虑如果您通过<a href='some_link'>Some link</a> 访问不同的页面并且需要将href 值更改为您定义的路由,如果没有,如果它们指向控制器,这仍然可以工作,但uri 名称将保留同样,我们以'some.beautiful-name'为例来访问MyController/notTheIndex
在你看来
<a href="<?=base_url()?>MyController/notTheIndex">This is a link</a>
链接有效,但 uri 名称将是 http://localhost/MyController/notTheIndex
<a href="<?=base_url()?>some-beautiful-name">This is a link</a>
链接会起作用因为你定义了这样的路由否则它不会并显示 404 错误,但这里显示的 url 将是 http://localhost/some-beautiful-name
希望我的回答对你有所帮助。