Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an admin section controllers and views in Code Igniter 2.0

I have created my site with controllers such as about, products etc... which gives me example.com/about/ etc..

How would I create a admin section with the same controller name, like example.com/admin/about or example.com/admin/products ?

How do I organize my controllers?

like image 522
Adam Avatar asked Dec 11 '25 17:12

Adam


2 Answers

2 more options to compliment WebweaverD answer.

  1. Use Modular Separation
  2. Create a second application folder and index file, that will be responsible for admin, connect them to the same system folder.

The second option is really easy to manage once you have set it up. There are variations but I find the structure below to be the most convenient.

mykewlwebsite.com
  apps/
    frontend/
      app/            frontend codeigniter application folder
      public/
        index.php
        assets/       frontend js, css, images
    backend
      app/            backend codeigniter application folder
      public/
        index.php
        assets/       backend js, css, images
  config/
    database.php
    constants.php
  system/             codeigniter system folder

The database.php file contains the code from CodeIgniter's config/database.php and is shared for all applications of your project - simply remove all code and add require_once('../../../../config/database.php'); to the config/database.php

index.php files inside public folders have two important variables $system_path and $application_folder, change them to

$system_path = '../../../system/codeigniter';
$application_folder = '../app';

constants.php file can have some constants like the ENVIRONMENT constant from the index.php files and some other. Just require_once() it from the index.php files.

Though there are pros and cons.

PROS

For those of you, who are developing on localhost and deploying to servers via FTP or other systems can simply upload one folder - apps and overwrite the target folder without fear of overwriting database.php settings (I'm sure most of you have them different from the local ones).

Adding one more application is easy - just duplicate one of existing. You can add as many applications as you want - api, ajax, user cabinet, etc.

CONS

This structure is meant to be used if you have a domain as mykewlwebsite.com and have the ability to add sub-domains to it, so you just configure the home folders for each of them:

mykewlwebsite.com: path/to/mykewlwebsite.com/apps/frontend/public/

admin.mykewlwebsite.com: path/to/mykewlwebsite.com/apps/backend/public/

api.mykewlwebsite.com: path/to/mykewlwebsite.com/apps/api/public/

like image 155
Sergey Telshevsky Avatar answered Dec 15 '25 21:12

Sergey Telshevsky


You have a few options here:

1) CREATE A SUBFOLDER - Put them in a folder called admin within the controllers directory (in application/contollers/admin/products.php)

A word of warning here is that you can only go one folder deep or codeigniter gets upset. Also, it will use first level controller/methods first so be careful of naming conflicts - e.g if you have an admin controller with a products method in it, that will get called before it looks in the admin directory for a products controller. (when going to example.com/admin/products)

2) USE THE ROUTES FILE - If it is just the urls you are worried about you could just call the controllers whatever you want and use the application/config/routes to redirect those paths to the controllers you want like this:

controller name: admin_products.php

routes file:

$route['admin/products'] = "admin_products";

3) USE A MASTER CONTROLLER FOR ALL - Final option would be to have a single admin controller and use named methods inside it, so for example you have admin.php controller with a products method within it this would then be called by admin/products uri (this will probably get messy though in a big application/site - not recommended)

Hope I have explained this OK for you, if you need any clarification please ask and I will try to elaborate.

like image 25
SwiftD Avatar answered Dec 15 '25 19:12

SwiftD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!