简体   繁体   English

创建codeigniter动态模板

[英]creating codeigniter dynamic template

i want to put login form in navigation(template) so that it will be shown in all the pages. 我想将登录表单放置在导航(模板)中,以便将其显示在所有页面中。 and will check username and password in database without going to other pages. 并将检查数据库中的用户名和密码,而无需转到其他页面。 if failed, an error will be shown in the nav and if success, the page will be reloaded again with specific costomizations. 如果失败,则导航栏中将显示错误;如果成功,则将使用特定的术语重新加载该页面。 please tell me how with code and details. 请告诉我如何使用代码和详细信息。

i have these parts in my template: 我的模板中有这些部分:

  1. header 标头
  2. nav 导航
  3. footer 页脚
  4. and content that will be added in current controller 和将添加到当前控制器中的内容

i wanna do this in each controller: 我想在每个控制器中执行此操作:

$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('related_view');
$this->load->view('templates/footer');

but my navigation should contain a login form which will not go to another page by submitting. 但我的导航中应包含一个登录表单,该表单不会通过提交进入另一个页面。

The solution I found is to extend the Controller class. 我发现的解决方案是扩展Controller类。 So In this case we should make an extended controller (for example MY_Controller) and put the dynamic action (here authentication) in it as a method. 因此,在这种情况下,我们应该制作一个扩展控制器(例如MY_Controller),并将动态操作(此处为身份验证)作为方法放入其中。

Then we should extend this self-made controller in each of our controllers , so authentication in the template would be simply handled. 然后,我们应该在每个控制器中扩展该自制控制器,以便可以轻松处理模板中的身份验证。

I usually do something like this: 我通常会这样:

<?php
$this->load->view('templates/header');

if ($this->user_model->isLoggedIn())
{
    // Logged-in users should see this view
    $this->load->view('templates/nav-registered');
}
else
{
    // Guests should see this
    $this->load->view('templates/nav-guest');
}

$this->load->view('related_view');
$this->load->view('templates/footer');

You'll thank yourself if you override the Loader class to grab your header/footer for you automatically, not having to code this in EVERY controller method. 如果您覆盖了Loader类以自动为您获取页眉/页脚,而不必在每个控制器方法中进行编码,您都会感激不尽。 Just a thought.. 只是一个想法..

use session to check loged user, and put this on the nav view, but first make sure you have session loaded from auload or from your controller 使用会话检查登录的用户,并将其放在导航视图中,但首先请确保已从auload或从控制器加载了会话

if($this->session->userdata('user_loged')){
      //code to put some navigation when use is loged
}else{
    //code to put login form
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM