点火器可以支持内联功能吗?


Can codeigniter support Inline functions.?

我们可以在Codeigniter的另一个函数中编写多个函数吗? 这是我的控制器

class Products extends CI_Controller {
  public function myproduct() {
      $this->load->view('myproduct'); // call myproduct.php
           public function features() {
              $this->load->view('features');  // call "myproduct/features"
            }
           public function screenshots() {
              $this->load->view('screenshots');  // call "myproduct/screenshots"
            }
    }
}

根据我的控制器,myproduct() 中有 2 个内联函数。 我的目标是将 URL 显示为

localhost/mysite/products/myproduct
localhost/mysite/products/myproduct/features
localhost/mysite/products/myproduct/screenshots

我已经试过了,但它给了我一个错误

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in D:'...........'application'controllers'mysite'products.php on line 5

而5号线是

public function features() { .........

您可以将其视为 url 中的 uri 参数:

public function myproduct($param = null) 
{
    if($param == null) {
        $this->load->view('myproduct'); 
    } elseif($param == 'features') {
        $this->load->view('features');
    } elseif ($param == 'screenshots') {
        $this->load->view('screenshots');
    }
}
这不是

代码点火器中的东西...这在 PHP 中通常是不可能的。您可以使用闭包,但它们不会在您的情况下呈现所需的效果。

尝试阅读 CodeIgniter URI 路由以了解代码点火器中的路由原则。而不是在控制器中创建单独的功能。

我不确定您要实现的目标,或者您计划如何调用/使用这些函数以及在哪个范围内,但为了在函数内声明函数,您这样做:

public function myproduct(){
    $t = 'myproduct';
    $features = function($t = '', &$this = ''){
        // some code goes here
        $this->load->view('features'); // will NOT work
        $this->load->view($t.'/features'); // this should work
    };
    $features($t, $this); // load the features view
}

不过,这应该是您的目标:

public function myproduct($uri_piece = ''){
    $this->load->view('myproduct/'.$uri_piece);
}