无法获得Codeigniter功能

时间:2014-03-07 20:15:53

标签: php codeigniter notice

目前此代码有效。

class Home extends MY_Controller {

public function index($renderData=""){  

    $this->title = "Needzilla";
    $this->keywords = "Needzilla";

            $this->_render('pages/register',$renderData);
    }

我在它下面添加它的那一刻......

public function home($renderData=""){   

    $this->title = "Needzilla";
    $this->keywords = "Needzilla";

            $this->_render('pages/home',$renderData);
}

这种情况发生了。

enter image description here

这指向这行代码。

    //data
    $toBody["content_body"] = $this->load->view($view,array_merge($this->data,$toTpl),true);

我正在试图弄清楚如何向控制器添加一个函数,这样当我调用该函数时它会打开另一个页面....例如......

从此页面获取(点击登录后)...

enter image description here

到这个页面...... enter image description here

Full My_Controller Code Here。

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class MY_Controller extends CI_Controller{

//Page info
protected $data = Array();
protected $pageName = FALSE;
protected $template = "main";
protected $hasNav = TRUE;
//Page contents
protected $javascript = array();
protected $css = array();
protected $fonts = array();
//Page Meta
protected $title = FALSE;
protected $description = FALSE;
protected $keywords = FALSE;
protected $author = FALSE;

function __construct()
{   

    parent::__construct();
    $this->data["uri_segment_1"] = $this->uri->segment(1);
    $this->data["uri_segment_2"] = $this->uri->segment(2);
    $this->title = $this->config->item('site_title');
    $this->description = $this->config->item('site_description');
    $this->keywords = $this->config->item('site_keywords');
    $this->author = $this->config->item('site_author');

    $this->pageName = strToLower(get_class($this));
}


protected function _render($view,$renderData="FULLPAGE") {
    switch ($renderData) {
    case "AJAX"     :
        $this->load->view($view,$this->data);
    break;
    case "JSON"     :
        echo json_encode($this->data);
    break;
    case "FULLPAGE" :
    default         : 
    //static
    $toTpl["javascript"] = $this->javascript;
    $toTpl["css"] = $this->css;
    $toTpl["fonts"] = $this->fonts;

    //meta
    $toTpl["title"] = $this->title;
    $toTpl["description"] = $this->description;
    $toTpl["keywords"] = $this->keywords;
    $toTpl["author"] = $this->author;

    //data
    $toBody["content_body"] = $this->load->view($view,array_merge($this->data,$toTpl),true);

    //nav menu
    if($this->hasNav){
        $this->load->helper("nav");
        $toMenu["pageName"] = $this->pageName;
        $toHeader["nav"] = $this->load->view("template/nav",$toMenu,true);
    }
    $toHeader["basejs"] = $this->load->view("template/basejs",$this->data,true);

    $toBody["header"] = $this->load->view("template/header",$toHeader,true);
    $toBody["footer"] = $this->load->view("template/footer",'',true);

    $toTpl["body"] = $this->load->view("template/".$this->template,$toBody,true);


    //render view
    $this->load->view("template/skeleton",$toTpl);
     break;
}
}
}

完整家庭课程

class Home extends MY_Controller {

public function index($renderData=""){  

    $this->title = "Needzilla";
    $this->keywords = "Needzilla";

            $this->_render('pages/home',$renderData);
}
 }

3 个答案:

答案 0 :(得分:0)

我认为您只需要为每个说法加载现有模板的不同视图,而不是路由到另一个uri。

假设您有“/ home /”

的路线

过去,我会创建一个名为“main_content”的动态区域的模板。它看起来像这样:

<html>
<head>
   ...
</head>
<body>
   ...   
   <?php $this->load->view($main_content); ?>
   ...
</body>
</html>

然后在我的控制器中,我有这样的事情:

<?php
   if( not_logged_in )
   {
       $data['main_content'] = 'views/login_form';
   }
   else
   {
       $data['main_content'] = 'views/dashboard';
   }

   $this->load->view('/views/templates/page', $data);

所有上述php逻辑都存在于同一路径中。根据用户是否登录,它只会显示两个不同的视图。

答案 1 :(得分:0)

您的函数名home与您的类/控制器名称相同,因此它会自动声明为类的构造函数,如php Constructors manual

中所述
  

构造函数是自动调用的类中的函数   当您使用new创建类的新实例时。一个功能变成了   构造函数,当它与类同名时。如果一个班级有   没有构造函数,将调用基类的构造函数if   它存在。

示例代码Auto_Cart()是构造函数,因为您的home函数成为类的构造函数,您需要更改函数的名称

class Auto_Cart extends Cart {
    function Auto_Cart() {
        $this->add_item("10", 1);
    }
}

您可以在此处找到的另一个例子Constructors and Destructors

  

从PHP 5.3.3开始,与a的最后一个元素同名的方法   命名空间的类名将不再被视为构造函数。这个   更改不会影响非命名空间的类。

示例代码Bar()将根据php版本

作为构造函数
namespace Foo;
class Bar {
    public function Bar() {
        // treated as constructor in PHP 5.3.0-5.3.2
        // treated as regular method as of PHP 5.3.3
    }
}

因此,当您尝试访问home()时,您正在调用构造函数,因此您的父类的构造函数应该处理load属性,但现在您已覆盖父构造函数,这会生成未定义的通知

答案 2 :(得分:0)

您应该在新班级中扩展父母的控制器。

根据codeigniter docs

“如果要扩展Controller核心类,请确保在应用程序控制器的构造函数中扩展新类。”

class Welcome extends MY_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->view('welcome_message');
    }
}