duanlu6114 2014-04-03 11:29
浏览 41
已采纳

Codeigniter登录表单在标题模板中

I'm using Codeigniter and the Ionauth library to manage users. The structure I'm using is as follows:

Controllers:
- home
- auth

Views:
- home
  - home_view.php

- templates
  - header.php
  - footer.php

- auth
  - login
  - register

When I create my home view, it adds the header.php and footer.php templates and passes it's own data into the main body.

My issue is, I'd like to use my login function within the header.php file (within a form), however, this isn't currently possible because my login function is situated in the /auth/ controller. Would it be possible for me to use this login function outside of the /auth/ controller, I.e. in the header.php file within the /templates/ folder?

If not, are there any possible solutions you can propose?

Thanks

  • 写回答

1条回答 默认 最新

  • dox19458 2014-04-03 12:49
    关注

    Yes you can do like this-

    Extend your core ci_controller to MY_Controller

    So, create your file - application/libraries/MY_Controller.php - and fill it with this code:

    <?php
    
    class  MY_Controller  extends  Controller  {
    
        function MY_Controller ()  {
    
    
    __construct()
    {
    parent::Controller();
    
    $this->load->view('templates/header')  // now in this way your header will get automatically included to every controller which extends MY_Controller
    
    }
        }
    //...
    }
    ?>
    

    Now extend your controllers to MY_Controller

    class  auth extends  MY_Controller  {
        function  __construct()  {
            parent::MY_Controller();
        }
    //...
    }
    
    class  home extends  MY_Controller  {
        function  __construct()  {
            parent::MY_Controller();
        }
    //...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效