簡體   English   中英

如何讓CodeIgniter忽略輸入到URL中的最后一個段 - 但仍保留在那里?

[英]How to get CodeIgniter to ignore the last segment entered into the URL-but still keep it there?

我正在使用CodeIgniter從頭開始構建用戶配置文件部分。 會發生什么,用戶將放入URL:

www.somesite.com/profile/view/<USERNAME>

配置文件是文件夾, 視圖是控制器)

我將使用IF ELSE語句來檢查$ currentURL (見下文)是否在數據庫中並加載所需的頁面。

但是現在正在發生的是它正在尋找一個函數(我認為)在視圖中執行。 但沒有。 這導致404錯誤。

CodeIgniter是否可以忽略該URL的最后一段但仍保留在那里以便我可以使用;

$currentURL = $this->uri->segment(3);

抓住USERNAME

感謝您查看我的問題,

劉易斯。

如果我理解正確的話:

聲明你的功能如下

\n

public function view($var1, $username = "") {...}

其中$var1必須填寫但$username可以省略,其默認值為""


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class View extends CI_Controller { //can not belive that View is not reserved word

    public function __construct() {
        parent::__construct();
    }

    public function index() {
        //index() function can not have parameters
        //redirect('view/show');
        //if no username is set, do default thing like show list of users
        //load view or something
    }

    public function show($username = "") {
        //this function can have parameters
        //load views from here
        $this->load->view('abc_view');
    }
}

添加到routes.php下面

$route['profile/view/(:any)'] = "profile/view/show/$1";

這將允許您按預期擁有漂亮的URL

site.com/profile/view/Peterson123

注意使用“路由”方法時不要在index() redirect(profile/view/show) index()


這里解釋 另一個使用_remap()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM