[英]static Url rewriting in Codeigniter
我的本地服务器中有一个Codeigniter应用程序。 我需要以下URL重写规则。 如果任何人有任何解决方案,请回答。
http://localhost/myapp/index.php/users/login
至
http://localhost/myapp/index.php/usuarios/login
我该如何在Codeigniter中执行此操作。
config/routes.php
此记录将执行您想要的操作-
$route['users/login'] = 'usuarios/login'; // for login only (static)
$route['users/(:any)'] = 'usuarios/$1'; // for all routes to users (dynamic for functions without parameters)
此处有更多详细信息: URI路由
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';
这是第一步
您创建了路线,但这些路线不起作用?
假设您有这个控制器
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 set 上第53行的route.php文件中所述
$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
$route['some-beautiful-name'] = 'MyController/notTheIndex';
因此,当您访问http:// localhost / some-beautiful-name时 ,它将显示MyController / notTheIndex的结果
考虑一下,如果u通过<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错误,但是此处显示的网址为http:// localhost / some-beautiful-name
希望我的回答对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.