简体   繁体   English

如何让CodeIgniter忽略输入到URL中的最后一个段 - 但仍保留在那里?

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

I'm using CodeIgniter to build a user profile section from scratch. 我正在使用CodeIgniter从头开始构建用户配置文件部分。 What happens is, the user will put in the URL: 会发生什么,用户将放入URL:

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

( profile is the folder & view is the controller) 配置文件是文件夹, 视图是控制器)

and I will use IF ELSE statements to check to see if $currentURL (see below) is in the DB and load the required page. 我将使用IF ELSE语句来检查$ currentURL (见下文)是否在数据库中并加载所需的页面。

But what's happening right now is it's looking for a function(I think) inside view to execute. 但是现在正在发生的是它正在寻找一个函数(我认为)在视图中执行。 But there is none. 但没有。 Which results in a 404 error. 这导致404错误。

Is it possible for CodeIgniter to ignore the last segment of that URL but still keep it there so that I can use; CodeIgniter是否可以忽略该URL的最后一段但仍保留在那里以便我可以使用;

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

to grab the USERNAME ? 抓住USERNAME

Thanks for viewing my question, 感谢您查看我的问题,

Lewis. 刘易斯。

If I understand you correctly: 如果我理解正确的话:

declare your function as follows 声明你的功能如下

\n

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

where $var1 must be filled in but $username can be ommited and its default value is "" 其中$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');
    }
}

Add to routes.php following 添加到routes.php下面

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

this will allow you to have nice URL as you expect 这将允许您按预期拥有漂亮的URL

site.com/profile/view/Peterson123

NOTE when using "routing" method do not redirect(profile/view/show) in index() 注意使用“路由”方法时不要在index() redirect(profile/view/show) index()


Another aproach that uses _remap() is explained here . 这里解释 另一个使用_remap()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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