簡體   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