繁体   English   中英

在Codeigniter中进行静态网址重写

[英]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';

这是第一步

您创建了路线,但这些路线不起作用?

  1. 在文件../application/config/config.php上设置base_url,在您的情况下为http:// localhost / myapp ,CI v3.1.6上文件的第26行
  2. 设置默认控制器,任何进入您的网站的人都会直接访问该默认控制器,您可以在../application/config/routes.php上进行设置 ,您的默认控制器可以是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';
    }
}

可以说此控制器将是您的默认控制器,如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. 现在您希望uris保留您设置的名称

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

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