所以我已经设置了我的codeigniter 3应用程序与chriskacerguis休息服务器正常工作,当我将它与jamieumberlow的MY_controller集成时,一切正常,除了加载视图时我得到重映射错误。那是我得到的错误
REST_Controller :: _ remap()的声明应该与MY_Controller兼容:: _ remap($ method)
我的代码示例如下
abstract class REST_Controller extends CI_Controller {
}
我的控制器扩展了其余的控制器
require APPPATH . '/libraries/REST_Controller.php';
class MY_Controller extends REST_Controller
{
}
和其他类扩展了MY_Controller
class Example extends MY_Controller {
}
问题是我无法让我的类使用重映射功能,我不想用重映射方法显示我的视图。它说这是不兼容的。已经有一段时间了。感谢任何帮助atm。
这是重新编写脚本,将其全部搞砸了
public function _remap($method)
{
if (method_exists($this, $method))
{
call_user_func_array(array($this, $method), array_slice($this->uri->rsegments, 2));
}
else
{
if (method_exists($this, '_404'))
{
call_user_func_array(array($this, '_404'), array($method));
}
else
{
show_404(strtolower(get_class($this)).'/'.$method);
}
}
$this->_load_view();
}
和加载视图功能
protected function _load_view()
{
if($this->_is_ajax())
{
$this->layout = FALSE;
if($this->_is_json())
{
$this->view = false;
//$this->output->set_content_type('application/json')->set_output(json_encode($this->data));
}
}
// If $this->view == FALSE, we don't want to load anything
if ($this->view !== FALSE)
{
// If $this->view isn't empty, load it. If it isn't, try and guess based on the controller and action name
$view = (!empty($this->view)) ? $this->view : $this->router->directory . $this->router->class . '/' . $this->router->method;
// Load the view into $yield
$data['yield'] = $this->load->view($view, $this->data, TRUE);
// Do we have any asides? Load them.
if (!empty($this->asides))
{
foreach ($this->asides as $name => $file)
{
$data['yield_'.$name] = $this->load->view($file, $this->data, TRUE);
}
}
// Load in our existing data with the asides and view
$data = array_merge($this->data, $data);
$layout = FALSE;
// If we didn't specify the layout, try to guess it
if (!isset($this->layout))
{
if (file_exists(APPPATH . 'views/layouts/' . $this->router->class . '.php'))
{
$layout = 'layouts/' . $this->router->class;
}
else
{
$layout = 'layouts/application';
}
}
// If we did, use it
else if ($this->layout !== FALSE)
{
$layout = $this->layout;
}
// If $layout is FALSE, we're not interested in loading a layout, so output the view directly
if ($layout == FALSE)
{
$this->output->set_output($data['yield']);
}
// Otherwise? Load away :)
else
{
$this->load->view($layout, $data);
}
}
}
答案 0 :(得分:0)
在codeIgniter的REST_Controller代码库中(如here所示),_remap方法具有以下签名:
public function _remap($object_called, $arguments)
在MY_Controller类中,您重新定义了此_remap方法,但使用了不同的签名:public function _remap($method)
问题是:如果不保留相同的签名(名称和参数),则无法从超类重新定义方法。在这里,您更改参数的数量,以便您的代码无法运行。
您应该更改_remap方法以使用原始参数,以解决此问题。
答案 1 :(得分:0)
也许您没有发送任何参数,如果是这种情况,则应将$ argument参数设为可选,如下所示:
public function _remap($method, $arguments=[]) {
}