代码点火器 :: 包括?

发布于 2024-10-03 10:31:17 字数 449 浏览 1 评论 0原文

我是代码点火器的新手,仍在学习最佳实践。

在我的网站中,我有两个菜单,

一个用于登录的用户,

一个用于注销的用户。

我将它们放在视图文件夹中的两个文件中。

我的问题是:

我应该如何将这些包括在内?

我最好的猜测是

在控制器调用中

if($loggedin)
  $menu = $this->load->view('loggedin', true);
else
  $menu = $this->load->view('loggedout', true);
$this->load->view('main', array('menu' => $menu));

,所以基本上将正确的菜单代码传递到视图中并回显出来。

有更好的方法吗?

I am new to code igniter and am still learning the best practices.

In my site I have two menus,

One for users who are logged in.

One for users who are logged out.

I have these in two files in the views folder.

My question is:

How should I go about including these?

my best guess is to

in the controller call

if($loggedin)
  $menu = $this->load->view('loggedin', true);
else
  $menu = $this->load->view('loggedout', true);
$this->load->view('main', array('menu' => $menu));

So basically pass the correct menu code into the view and echo that out.

Is there a better way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-10-10 10:31:17

你可以打破严格的 MVC 并在你的观点中拥有类似的东西:

<?php
// in your controller or when the user logs in:
$this->session->set_userdata('logged_in', 'true');
?>

你的观点:

<?php
  // Logged in?
  if ( ! $this->session->userdata('logged_in') {
      $this->load->view('loggedout');
  } else {
      $this->load->view('loggedin');
  }
?>

记住 MVC 是一个概念,它不是一成不变的。

you can break strict MVC and have something like this in your view:

<?php
// in your controller or when the user logs in:
$this->session->set_userdata('logged_in', 'true');
?>

your view:

<?php
  // Logged in?
  if ( ! $this->session->userdata('logged_in') {
      $this->load->view('loggedout');
  } else {
      $this->load->view('loggedin');
  }
?>

remember MVC is a concept, it isn't set in stone.

走野 2024-10-10 10:31:17

您可以在函数中加载任意数量的视图。例如:

if($loggedin){
  $this->load->view('loggedin');
} else {
  $this->load->view('loggedout');
}

$this->load->view('main');

使其变得更清洁,但效率节省可能可以忽略不计。

You can load as many views as you like in the function. So for example:

if($loggedin){
  $this->load->view('loggedin');
} else {
  $this->load->view('loggedout');
}

$this->load->view('main');

Makes it a little cleaner but probably negligible efficiency savings.

宛菡 2024-10-10 10:31:17

我建议你使用插件。

将其放入插件中

if($loggedin)
  $menu = $this->load->view('loggedin', true);
else
  $menu = $this->load->view('loggedout', true);

I would suggest you to use plugins.

put this in to plugin

if($loggedin)
  $menu = $this->load->view('loggedin', true);
else
  $menu = $this->load->view('loggedout', true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文