在 CodeIgniter 中制作管理页面的最佳方法?

发布于 2024-08-11 00:37:56 字数 403 浏览 5 评论 0原文

我正在 CodeIgniter 中开发一个应用程序,我希望为应用程序中的几个对象提供管理页面,我想知道将这些对象放入 MVC 结构中的更好方法是什么。

想法1: 在每个控制器中,都有一个管理功能,并将我想要的所有管理页面添加到该功能中。 示例 URL:domain.com/articles/admin

想法 2 创建一个新的管理控制器,它必须引用许多不同的模型,并将所有管理页面放在那里。 示例 URL:domain.com/admin/articles

哪种方式更好?

编辑澄清:通过管理功能,我的意思是能够对任何对象执行基本的 CRUD 操作,并能够显示所有所述对象的列表。

I'm working on an app in CodeIgniter, and I want to have admin pages for several of the objects in the application, and I'm wondering what would be the better way to put these into an MVC structure.

Idea 1:
In each controller, have an admin function, and add all of the admin pages I would like into that function.
example URL: domain.com/articles/admin

Idea 2
Make a new admin controller, which would have to reference many different models, and put all of the admin pages in there.
example URL: domain.com/admin/articles

Which way would be better?

Edit for clarification: By admin functionality, I mean being able to do the basic CRUD actions on any object, and be able to display a list of all of said object.

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

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

发布评论

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

评论(7

萌无敌 2024-08-18 00:37:56

绝对是不同的控制器至少!

我曾经认为我可以将所有管理功能保留在一个控制器中,但随着我的程序的增长,我意识到我的管理部分需要多个控制器。

因此,我在控制器文件夹中创建了一个名为“admin”的文件夹,并将所有管理控制器放入其中。所以我的文件夹看起来像:

  • 应用程序
    • 控制器
      • front.php
      • welcome.php
      • 管理员
        • dashboard.php
        • useradmin.php
  • 等等...

然而,当您输入 http://mysite.com/admin 在浏览器中,它会返回 404 页面。因此,转到“application/config/routes.php”文件并添加自定义路由:

$routes['admin'] = 'admin/dashboard/index';

Definitely a different controller at least!

I used to think that I could keep all my admin functions in a single controller, but as my programs grew, I realized that I needed multiple controllers in my administration section.

So, I created a folder inside my controllers folder with the name "admin" and put all my administrative controllers in there. So my folders would look something like:

  • application
    • controllers
      • front.php
      • welcome.php
      • admin
        • dashboard.php
        • useradmin.php
  • etc...

One problem this creates, however, is when you type http://mysite.com/admin in your browser, it returns a 404 page. So, go to your "application/config/routes.php" file and add a custom route:

$routes['admin'] = 'admin/dashboard/index';
作妖 2024-08-18 00:37:56

我会同意贾斯汀的观点,将其保留为单独控制器的一部分。

您应该设置某种授权系统,各个控制器可以使用该系统来确定谁登录(用户名)以及他们拥有什么访问权限(管理员/成员/等)。 这是一个关于 CodeIgniter 身份验证类的主题

然后,视图将有条件地显示适当的链接,并且控制器将通过在将任何数据传递到模型或渲染编辑视图之前检查身份验证来强制执行策略。在未经授权的访问上,可能会呈现错误,或者只是使用非编辑视图进行呈现。

这种方法似乎最有意义(至少对我来说),因为所有功能都存储在单独的控制器中。将管理功能保留在单个管理控制器中意味着每次添加新内容(或删除内容)时,您都必须管理两个控制器(管理员和实际控制器)。

如果您担心在每个控制器中进行身份验证检查,您可以创建一个包含所有身份验证设置的通用控制器类,然后让您的控制器扩展它。最后,单独的控制器身份验证检查可能很简单:

function edit()
{
    if(!$this->auth()){
        //display auth error, or forward to view page
    }
}

当然,某种 ACL 实现会让这更好,但我不相信 CodeIgniter 有“官方”ACL。

I'll echo Justin in keeping it part of the individual controllers.

You should setup some kind of authorization system that the individual controllers can use to so who is logged in (username) and what access they have (admin/member/etc). Here's a SO thread on CodeIgniter Auth Classes.

The view would then conditionally show the appropriate links, and the controller would enforce the policy by checking the auth before passing any data to the model or rendering an edit view. On unauthorized access an error could be rendered, or simply render with the non-editing view.

This approach seems to make the most sense (at least to me) because all the functionality is stored in the individual controller. Keeping admin functions in a single admin controller means you'll have to manage two controllers (the admin, and the actual controller) every time you add somethign new (or remove something).

If you're concerned about putting auth checking in every controller, you could create a generic controller class with all the auth setup, then have your controllers extend it. In the end the individual controller auth check could be as simple as:

function edit()
{
    if(!$this->auth()){
        //display auth error, or forward to view page
    }
}

Of course some kind of ACL implementation would make this better, but I don't believe CodeIgniter has an 'official' ACL.

南街女流氓 2024-08-18 00:37:56

最好在控制器文件夹中有一个管理文件夹,您可以在其中访问您的管理,例如 yoursite.com/admin/users。

您的所有管理需求都将在那里,并且所有方法都将通过检查用户权限来受到保护,如下所示:

if ( ! $this->auth->logged_in(array('login', 'admin')))
{
    $this->session->set_flashdata('message', 'You do not have access to view this page');

    redirect('admin/users/login');
}

然后“admin”文件夹之外的所有控制器将 - 取决于您的站点类型 - 将仅用于查看等......没有管理部分。

It's a good idea to have an admin folder in the controllers folder wherein you can access your administration e.g. yoursite.com/admin/users.

All your administrative needs will be there and all methods will be protected by checking user privileges like so:

if ( ! $this->auth->logged_in(array('login', 'admin')))
{
    $this->session->set_flashdata('message', 'You do not have access to view this page');

    redirect('admin/users/login');
}

Then all controllers outside the 'admin' folder will - depending on your type of site - will only be for viewing, etc.. no administrative portions.

木緿 2024-08-18 00:37:56

想法2更好。
system/application/controllers/admin

您将所有管理控制器保存在这里。

Idea 2 is better.
system/application/controllers/admin

You keep all your admin controllers here.

小忆控 2024-08-18 00:37:56

Here is an extensive guide to the pro's and con's of each method:

http://philsturgeon.co.uk/news/2009/07/Create-an-Admin-panel-with-CodeIgniter

滥情空心 2024-08-18 00:37:56

根据您所说的“管理”功能的含义...通常,这被认为是“编辑”视图。

通常,您使用现有控制器来提供“编辑”视图,允许授权用户进行编辑(在您的情况下,仅限管理员用户)。

Depending on what you mean by 'Admin' functionality...typically, this is thought of as an 'Edit' view.

And typically, you use the existing controller to serve the 'Edit' view allowing the authorized users to make the edits (in your case, Admin users only).

烟燃烟灭 2024-08-18 00:37:56

看起来像是个人选择,我喜欢将一切集中起来,所以管理控制器将是我的选择。

这样我就不必在修改管理任务时打开 5 个不同的控制器。

Looks like a personal choice, i love having everything centralized so the admin controller would be my bet.

That way i wouldn't have to open up 5 different controllers while modifying admin tasks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文