如何从Codeigniter中的Controller方法获取参数?

时间:2018-11-01 17:18:45

标签: php codeigniter-3

我尝试了以下使用路由和控制器的方法,并尝试从controller方法获取参数值,但得到的结果为$ 1。但实际上,我通过了3。

routes.php
    $route['helloword/:any'] = "FirstProgram/helloworld/$1";

    controller
    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class FirstProgram extends CI_Controller {
      public function helloworld($param=''){
        $data = array(
          'title'   => 'Title',
          'heading' => 'Heading',
          'message' => 'Message',
          'param'   => $param
        );
        echo $param;exit;
        $this->load->view('FirstProgram', $data);
      }
    }
    ?>

enter image description here

1 个答案:

答案 0 :(得分:3)

更改您的路线代码,只需为:any添加一个括号

$route['helloword/(:any)'] = "FirstProgram/helloworld/$1";