[英]static Url rewriting in Codeigniter
I have a codeigniter application in my localserver. 我的本地服务器中有一个Codeigniter应用程序。 I need some URL rewriting rule for the following. 我需要以下URL重写规则。 If any one have any solution please answer. 如果任何人有任何解决方案,请回答。
http://localhost/myapp/index.php/users/login
to 至
http://localhost/myapp/index.php/usuarios/login
How can I do this in Codeigniter. 我该如何在Codeigniter中执行此操作。
This record in config/routes.php
will do what you want - 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)
More details here : URI Routing 此处有更多详细信息: URI路由
Codeigniter works like this to access myFunction from Mycontroller with the value myVarValue Codeigniter的工作类似于从Mycontroller访问myFunction ,其值为myVarValue。
http://my.webPage.com/index.php/MyController/myFunction/myVarValue
U want to alter the URL, yes you can by creating routes on ../application/config/routes.php 你想改变URL,是的,你可以在../application/config/routes.php上创建路由
$route['my-function-(:any)'] = 'MyController/myFunction/$1';
This is the first step 这是第一步
You created the routes and those are not working? 您创建了路线,但这些路线不起作用?
Let's say u have this controller 假设您有这个控制器
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';
}
}
Lets say this controller will be ur default controller so as mentioned in the routes.php file in the line 53 on ci v3.16 set 可以说此控制器将是您的默认控制器,如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';
so when u access http://localhost/some-beautiful-name it will show the result of MyController/notTheIndex 因此,当您访问http:// localhost / some-beautiful-name时 ,它将显示MyController / notTheIndex的结果
Consider that if u are accessing the different pages via a <a href='some_link'>Some link</a>
and the href values need to be changed to your defined routes, if not, this will still work if they are pointing to controllers, but the uri names will stay the same, lets take the example of 'some.beautiful-name' to access MyController/notTheIndex 考虑一下,如果u通过<a href='some_link'>Some link</a>
访问不同的页面,并且href值需要更改为您定义的路由,如果没有更改,则当它们指向时仍然可以使用控制器,但uri名称将保持不变,让我们以“ some.beautiful-name”为例来访问MyController / notTheIndex
In Your View 在您看来
<a href="<?=base_url()?>MyController/notTheIndex">This is a link</a>
The link will work but the uri name will be http://localhost/MyController/notTheIndex 该链接将起作用,但uri名称将为http:// localhost / MyController / notTheIndex
<a href="<?=base_url()?>some-beautiful-name">This is a link</a>
The link will work because u defined a route like this else it will not and show 404 error, but here the url shown will be http://localhost/some-beautiful-name 该链接将起作用, 因为您定义了这样的路由,否则将不会显示404错误,但是此处显示的网址为http:// localhost / some-beautiful-name
Hope my answer helps u. 希望我的回答对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.