`
天梯梦
  • 浏览: 13746395 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

Create an Admin panel with CodeIgniter

阅读更多

Create an Admin panel with CodeIgniter

 

As I see it there are three methods to creating an admin system using the MVC framework CodeIgniter . In this article I will show examples of the structures for each and mention the pro's and con's of using each.

This article will only outline the theory and suggest the structures to you. I do not plan on writing yet another "How to make a user login system and add admins to it" type article.

 

1.) Two applications

 

In CodeIgniter you can easily set up multiple applications to run off the same CodeIgniter install, simply by creating a copy of index.php and renaming it to something else.

 

/
  applications/
    frontend/
        controllers/
         home.php
            blog.php
            comments.php
     models/
         blog_model.php
         comment_model.php
     views/
         blogs/
             index.php
             view.php
         comment/
             view.php
         index.php

    backend/
     config/
     controllers/
         dashboard.php
            blog.php
            comments.php
     models/
         blog_model.php
         comment_model.php
     views/
         blogs/
             index.php
             form.php
         comment/
             index.php
         dashboard.php
         login.php

  system/

  index.php
  admin/
    index.php

 

Here you can see I have put index.php into an admin/ folder of its own. Both index.php files will point to a single folder within /applications and this can be done by setting:

 

 

index.php

 

$application_folder = "applications/frontend"; 
 

 

admin/index.php

 

$application_folder = "applications/backend"; 
 

 

This method does work, but is only really any good for big sites that have very different content for their front and back ends. You cannot use the same libraries, helpers, models, etc which will mean its very easy to end up with duplicated code. I'm not a big fan of such frontend/backend separation as for most sites, an admin panel will use the same models and code but this varies entirely on the spec of the site you are building.

 

2.) Sub-directories

 

This method follows a more usual CodeIgniter set-up and is the way that most new CodeIgniter users will try things at first.

 

/
    application/
        config/
        controllers/
            admin/
                 blog.php
                 comments.php
            blog.php
            comments.php
        models/
            blog_model.php
            comments_model.php
        views/
            admin/
                blog/
                    index.php
                    form.php
                comments/
                    index.php
                    form.php
                dashboard.php
                login.php
            blog/
                index.php
                view.php
            comments/
                view.php
  system/
  index.php

 

Here we are keeping the default MVC structure of CodeIgniter and using sub-directories for our controllers to give us the http://example.com/admin/blog URL structure. This method has the advantage of being able to share models, libraries and helpers across both the front and backend. If you really need to separate models for front and back ends, why not just have a models/admin/ folder and put them in there?

The down side is that when your site expands and more controllers are required, it can be a real pain to have your content so loosely linked across the entire application directory. You can see in the example above that we have several folders for blog and comment content, where really we should only have one. This one folder is called a module...

 

3.) Modules

 

To keep all the content under one single folder we can adopt the HMVC approach. This stands for Hierarchal MVC which essentially is just modular CodeIgniter. Two systems have been developed for this: HMVC and Matchbox . I personally prefer use the latter but have never tried HMVC so i'll leave that one up to you.

A strange thing that many CodeIgniter users seem to do is create a blog module, comment module and admin module. This strikes me as a very strange separation of content that goes against the point of using modules in the first place! I have a single admin.php controller in the main controllers folder to act as the default admin page which will handle login, logout and the main dashboard. Then I add another admin.php controller in each module and use URI Routing to get my URL structure as http://example.com/admin/.

 

/
    application/
        config/
        controllers/
             admin.php
        modules/
             blog/
                 controllers/
                     admin.php
                     blog.php
                 models/
                     blog_model.php
                 views/
                     admin/
                         index.php
                         form.php
             comments/
                  controllers/
                      admin.php
                      comments.php
                  models/
                      comment_model.php
                  views/
                     admin/
                         index.php
                         form.php
          views/
              admin/
                  dashboard.php
                  login.php
      system/
      index.php

 

Right now to get at the blog admin you would have to go to http://example.com/blog/admin which may well be fine with you. If not, you can add the following routes to your application/config/routes.php to swap the segments around and emulate a /admin/ folder.

 

$route['admin/([a-zA-Z_-]+)/(:any)'] = '$1/admin/$2';
$route['admin/login'] = 'admin/login';
$route['admin/logout'] = 'admin/logout';
$route['admin/([a-zA-Z_-]+)'] = '$1/admin/index';
$route['admin'] = 'admin'; 
 

 

This way you have your admin controllers kept with the frontend controllers, you are sharing models, libraries and helpers and you still have some nice URL's.

 

Summary

 

If your front and back end applications share nothing in common and never will do, use method #1. If you have a small site with one a few controllers and do not want the small overhead HMVC adds, use method #2. If you are working on a massive site that is modular and shares code between front and back ends, use method #3.

Got any more methods to handle admin structures? Let me know in the comments.

 

 

 

 

分享到:
评论

相关推荐

    CodeIgniter-admin-panel, 使用 Twitter Bootstrap的管理员面板的CodeIgniter模板.zip

    CodeIgniter-admin-panel, 使用 Twitter Bootstrap的管理员面板的CodeIgniter模板 CodeIgniter-Sample-Project使用 Twitter 2.1.2和 Bootstrap的示例管理员面板。我想让大家知道,想法是接受建议,并且随着社区的...

    CodeIgniter-Admin-Panel

    "CodeIgniter-Admin-Panel"项目是基于这个框架创建的一个后端管理面板。这个管理面板使用了CodeIgniter 3版本,它是CodeIgniter的一个稳定版本,提供了许多优化和改进,包括更好的性能和增强的安全性。 在描述中...

    基于 vue-element-admin 和 PHP CodeIgniter RES-vue-php-admin.zip

    Vue-element-admin 和 PHP CodeIgniter 是两种非常流行的开源框架,分别用于前端开发和后端构建。这个名为 "RES-vue-php-admin" 的压缩包显然整合了这两种技术,旨在创建一个全面的后台管理系统。 Vue-element-...

    Codeigniter-php-Bootstrap-AdminLTE-Panel-with-user-management:具有用户管理功能的Codeigniter Bootstrap管理面板

    Codeigniter-php-Bootstrap-Admin-Panel和用户管理 具有用户管理功能的Codeigniter Bootstrap管理面板 Bootstrap是最流行HTML,CSS和JS框架,用于在Web上开发响应式,移动优先项目。 它是为自定义CodeIgniter项目...

    SatSun CRMS beta0.1(CodeIgniter)

    SatSun CRMS beta0.1(for CodeIgniter) SatSun CRMS beta0.1(with CodeIgniter) SatSun CRMS beta0.1(for CodeIgniter) SatSun CRMS beta0.1(with CodeIgniter)

    CodeIgniter

    "CodeIgniter 2.1.2"是该框架的一个特定版本,它在2012年发布,虽然现在已经有了更新的版本,但这个老版本仍然被许多开发者用于维护旧项目或者教学用途。 CodeIgniter基于Model-View-Controller(MVC)设计模式,这...

    codeigniter使用例程 网站跳转管理系统

    这个“codeigniter使用例程 网站跳转管理系统”是一个基于CodeIgniter的简单应用,它利用MVC(模型-视图-控制器)设计模式来组织代码结构。下面我们将详细探讨这个例程中的关键知识点。 1. MVC架构: MVC是一种...

    CodeIgniter-3.1.10:vue-php-admin-V3 项目后台API接口

    element-admin 和 PHP CodeIgniter RESTful 实现,采用前后端分离架构的权限管理系统,PHP快速开发平台,目标是搭建一套简洁易用的快速解决方案,可以帮助用户有效降低项目开发难度和成本。以vue-element-admin@...

    CodeIgniter Web Application Blueprints

    Loosely based on the Model-View-Controller pattern, CodeIgniter is an open source framework for application development. It allows you to develop projects by providing a rich set of libraries for ...

    PHP框架之CodeIgniter留言板实例

    PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter留言板实例PHP框架之CodeIgniter...

    codeigniter的简单示例

    在这个"codeigniter的简单示例"中,我们将深入探讨CodeIgniter的核心概念和基本用法。 1. **MVC架构**: MVC是软件设计中的一个经典模式,用于分离业务逻辑、数据处理和用户界面。在CodeIgniter中,模型(Model)...

    Codeigniter-login-auth-admin

    Source Server : mysql_localhot Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : codeigniter Target Server Type : MySQL Target Server Version : 50624 ...

    CodeIgniter2.0 中文手册

    以下是对CodeIgniter2.0的一些核心知识点的详细解释: 1. MVC架构:CodeIgniter遵循Model-View-Controller(MVC)设计模式,将业务逻辑、数据处理和用户界面分离,提高代码的可维护性和可扩展性。Model负责处理数据...

    codeigniter 代码实例分享

    这个"codeigniter 代码实例分享"看起来是某个开发者或团队为了展示或分享他们在CodeIgniter上的工作成果,结合了jQuery 1.4、Eclipse IDE的使用经验。 在这些提供的文件中,我们可以看到一些关键组成部分: 1. **...

    CodeIgniter3中文手册

    CodeIgniter以其小巧、高性能、易于使用和配置、不需要命令行、无需新的模板语言、以及简化的编码规则等特点受到开发者的青睐。CodeIgniter的核心思想是减少编码工作量,让开发者能够把精力集中在创新性工作的开发上...

    codeigniter-3.2.1-with-admin-LTE-Template-Intigration:带有php Codeigniter 3.2.1的Admin LTE Templete最新版本

    codeigniter-3.2.1-with-admin-LTE-Template-Intigration带有php codeigniter 3.2.1的最新版Admin LTE Templete 从存储库下载代码。 解压缩该zip文件。 打开浏览器; 转到localhost / phpmyadmin。 在该数据库中导入...

    CodeIgniter1.7敏捷框架开发

    《CodeIgniter1.7敏捷框架开发》一书由Jose Argudo Blanco与David Upton共同撰写,由Packt Publishing在2009年11月首次出版。这本书旨在帮助PHP开发者提升编码效率,通过免费、紧凑且开源的MVC框架——CodeIgniter...

    codeigniter 2.0 中文手册

    本资源提供的"CodeIgniter 2.0 中文手册"是针对该框架的详细指南,帮助开发者更好地理解和运用这个框架。 CodeIgniter的核心设计理念是"简单而强大",它提供了一个优雅的PHP接口,让开发者可以构建功能丰富的Web...

    CodeIgniter 相关文档资料

    这个压缩包包含的资源对于深入理解和使用CodeIgniter至关重要。以下是对这些资源的详细解析: 1. **CodeIgniter2.2.0中文用户参考手册.CHM**: 这个CHM文件是CodeIgniter 2.2.0版本的中文用户手册,它包含了框架的...

Global site tag (gtag.js) - Google Analytics