如何在codeigniter中的baseurl之后传递参数?

时间:2018-12-27 11:54:23

标签: php codeigniter

基本URL:http://localhost/f1/

,并且我在URL中传递参数,例如:     http://localhost/f1/user1

我正在尝试打印

function index(){
    echo $this->uri->segment(2);
}

我想在控制器中打印User1。如何实现呢?

6 个答案:

答案 0 :(得分:1)

配置您的溃败并添加以下内容:

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

在这里,您随身携带控制器

class Controller_Name extends CI_Controller {
  function index($prm){
    echo $prm;
  }
}

玩得开心.... 您可以拥有多少个您想要的尿....

答案 1 :(得分:0)

在此处阅读https://www.codeigniter.com/userguide3/general/controllers.html-> Passing URI Segments to your methods

/products/shoes/sandals/123

<?php
class Products extends CI_Controller {

        public function shoes($sandals, $id)
        {
                echo $sandals;
                echo $id;
        }
}

答案 2 :(得分:0)

base_url项/参数之后可以这样

确保分别在base_urlapplication/config/config.php文件中设置了application/config/autoload.php和URL帮助器

假设,调用带有 anchor标记

的函数
<a href="<?= base_url('HomeController/index/'.$url1)?> ">Index</a>

在上一行中,HomeController是控制器名称,函数名称中的index和参数$url1

class HomeController extends CI_Controller {

    public function index($url1){
         echo $url1;
    }
}

还可以使用$this->uri->segment()

答案 3 :(得分:0)

我从您的问题中了解到的是,您希望不使用方法重新映射并在控制器后直接传递参数?

您可以使用_remap并检查它是否与正常处理的方法匹配,或直接将其传递给默认方法,现在您无需按预期在URL中使用index

public function _remap($method)
{
        if ($method === 'some_method_in_your_controller')
        {
                $this->$method();
        }
        else
        {
                $this->index($method);
        }
}

现在,假设您的网址类似于http://localhost/controller/parameter,然后,如果此参数与某个方法匹配,它将调用该方法;否则,它将作为参数传递给您index

答案 4 :(得分:0)

在config / routes.php中定义您的控制器。它将默认调用索引函数。

$route['default_controller'] = 'controllername';

答案 5 :(得分:0)

检查是否加载了URL帮助器。只有这样,它才能按预期工作。自动加载它或在控制器中调用它。