【问题标题】:How to read parameter of URL from default controller如何从默认控制器读取 URL 参数
【发布时间】:2021-03-13 15:20:36
【问题描述】:

我想从默认控制器上的 url 中捕获参数,但它给 ma 一个错误 404 page not found.

routes.php

$route['default_controller'] = 'MainBrain';
$route['MainBrain/(:any)'] = "MainBrain/index/$1";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

MainBrain .php

class MainBrain extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
    }

    public function index() {
        echo 'Parameter value - ' . $this->uri->segment(3);
        $this->load->view('index', $data);
    }

}

现在每当我尝试加载 url localhost:/myproject => 这正在加载我的默认控制器及其视图,但是当我尝试 localhost:/myproject/pa8989 => 这给了我 404 page not found 错误。

.htaccess:

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

那么我怎样才能得到参数形式的默认参数而不是 404 错误呢?

【问题讨论】:

    标签: php .htaccess codeigniter


    【解决方案1】:

    如果您希望 localhost:/myproject/pa8989 路由到您的函数,那么您需要更改路由:

    $route['MainBrain/(:any)'] = "MainBrain/index/$1";
    

    $route['(:any)'] = "MainBrain/index/$1";
    

    否则,您需要通过 localhost:/myproject/mainbrain/pa8989

    访问该函数

    【讨论】:

    • 感谢您的回答,但这不起作用。因为如果将调用任何其他控制器,它将仅重定向到默认控制器。示例 - localhost:/myproject/othercontroller/ => 然后它将仅重定向到默认控制器。
    猜你喜欢
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    相关资源
    最近更新 更多