1

我们可以在 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 个内联函数。我的目标是将网址显示为

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() { .........
4

3 回答 3

0

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

尝试阅读CodeIgniter URI Routing以了解 codeigniter 中的路由原理。比在控制器中创建单独的功能。

于 2014-11-14T13:03:22.663 回答
0

您可以将其视为 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');
    }
}
于 2014-11-14T13:05:17.083 回答
0

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

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);

}
于 2014-11-14T14:45:59.357 回答