简体   繁体   English

在Codeigniter中进行静态网址重写

[英]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? 您创建了路线,但这些路线不起作用?

  1. Set the base_url on the file ../application/config/config.php , in your case will be http://localhost/myapp , line number 26 of the file on CI v3.1.6 在文件../application/config/config.php上设置base_url,在您的情况下为http:// localhost / myapp ,CI v3.1.6上文件的第26行
  2. Set the default controller, the default controller that will be inmediatelly accessed by anyone who enters ur website, u set this on ../application/config/routes.php , your default controller can be the name of a Controller or A function within a controller. 设置默认控制器,任何进入您的网站的人都会直接访问该默认控制器,您可以在../application/config/routes.php上进行设置 ,您的默认控制器可以是Controller的名称,也可以是控制器。

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
  1. Now u want the uris to keep have the names u set 现在您希望uris保留您设置的名称

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM