Separate Admin and Front in codeigniter - codeigniter

What is the best way to separate admin and front-end for a website in codeigniter where as I was to use all libraries, models, helpers etc. in common, but only controllers and Views will be separate.
I want a more proper way, up for performance, simplicity, and sharing models and libraries etc.

I highly suggest reading the methods outlined in this article by CI dev Phil Sturgeon:
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
My advice: Use modules for organizing your project.
https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
Create a base controller for the front and/or backend. Something like this:
// core/MY_Controller.php
/**
* Base Controller
*
*/
class MY_Controller extends CI_Controller {
// or MX_Controller if you use HMVC, linked above
function __construct()
{
parent::__construct();
// Load shared resources here or in autoload.php
}
}
/**
* Back end Controller
*
*/
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Check login, load back end dependencies
}
}
/**
* Default Front-end Controller
*
*/
class Public_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
// Load any front-end only dependencies
}
}
Back end controllers will extend Admin_Controller, and front end controllers will extend Public_Controller. The front end base controller is not really necessary, but there as an example, and can be useful. You can extend MY_Controller instead if you want.
Use URI routing where needed, and create separate controllers for your front end and back end. All helpers, classes, models etc. can be shared if both the front and back end controllers live in the same application.

I use a very simple approach: file folders. Check out the CI User Guide section, Organizing Your Controllers into Sub-folders.
I have my public-facing website built as any other would be built with CodeIgniter. Then I have two additional folders, controllers/admin and views/admin.
The admin controllers are accessed via http://[hostname]/admin/controller, and behave just as any other controller except they have specific authentication checks. Likewise, the views are simply called with the folder name included: $this->load->view('admin/theview');.
I haven't found a reason to do anything more complicated than that.

You all can find complete solution over here, https://github.com/bhuban/modular
Module separation for admin and front-end using HMVC and template separation using template libraries
I am using two third party libraries, you can find it in zip file.
HMVC for modular developed by wiredesignz
Template engine for templating by Phil Sturgeon
Just unzip it into your webserver root directory and run
localhost/modular for front-end
and
localhost/modular/admin for back-end
application/back-modules, it is for the back-end modules
application/front-modules, it is for the front-end modules
similarly
templates/admin for the back-end templates
templates/front for the front-end templates
themes/admin for the back-end themes
themes/front for the front-end themes
Nothing hacked in original code just configured using config.php and index.php

Related

How to implement Medoo in Codeigniter v.3

I am not able to make Codeigniter v3.1.8 work with Medoo DB framework. Is there any instructions how to do that? Should i just include Medoo lib or preconfigure Codeigniter settings?
As the Medoo php file is namespaced, uses use statements, and contains another class Raw you cannot load it as a library (at least not until CI4 rolls out). It would probably be the easiest to just require the file as necessary or to use composer to autoload it (which CI supports).
Otherwise you could just create a MY_Controller in application/core and require it at the top of the file e.g.:
require(APPPATH . 'third_party/Medoo.php');
class MY_Controller extends CI_Controller {
}
and then any controllers that need it should extend MY_Controller.
Alternatively, you can follow my answer here (technique to load namespaced libraries): How do I include the OOP "defiant randomdotorg" library in codeigniter?

Basic way to do user authentication for codeigniter 3

My current solution is to:
Create a authentication_model.
and write my authentication in the __constructor of each controller that needs a user logged in. Very simple and easy to understand.
My other option is to make a My_controller and do my authentication there and extend it with all controllers that need a user logged in.
Problem is, the answers ive gotten from this varies, some only use My_controller but some have base_controller and auth_controller.
And i'm confused if a single My_controller for authentication is enough or not.
Let's assume you are using Ion_Auth (I can recommend it). Now, if your site is 99% behind an auth system a MY_Controller with a __construct that looks like this should be sufficient for all of the controllers requiring auth:
class MY_Controller extends CI_Controller {
public function __construct() {
if (!$this->ion_auth->logged_in()) {
redirect('login');
}
}
}
and then in the login controller (e.g. user isn't logged in yet) you simply extend CI_Controller so you don't end up in a redirect loop.
The issue with this approach becomes apparent if you are like me and have separate frontend and backend running on the same CI installation. In such a case you have you public controllers in your main CI controllers directory and a sub-directory in controllers called auth. Again, in my case, the frontend and backend share similar resources but I don't want to bog down the frontend by autoloading Ion_Auth and the users_model and all the models that make my backend work as its not required functionality.
So I only autoload things I use in both such as sessions and database and then have a Shared_Controller that extends CI_Controller that sets up some public variables that I use in both. Then I have a Auth_Controller that extends the Shared_Controller and uses a similar approach as the code above; in that __construct I load what I would have autoloaded had I not had a "separate" frontend (such as Ion_Auth .etc.) and just make the login controller a special case that does not get redirected.
I then also have a Frontend_Controller also extending Shared_Controller that loads some things I use throughout the frontend controllers.
So as you can see it largely depends on what you are trying to accomplish. There is no right or wrong way just left and right.

sharing template between frontend and backend (admin or adminhtml) in magento

I wish to reuse a certain frontend UI element on the backend (under "design/adminhtml"). This would mostly consist of reusing the template (phtml). However, referencing the frontend layout handle from the backend would seem even better. Does magento provide a location for shared UI components, a way to declare them as shared, or a mechanism for referencing them across the frontend/adminhtml divide? Thanks
#coriscus
Yes that is possible. I found the trick you use frontend template from admin.
public function __construct()
{
parent::__construct();
$this->setData('area','frontend');
$this->setTemplate('customer/online.phtml');
}
just set needed area in block constructor.

Zend multiple directories and MVC routing

I'm building my first personal website using Zend 1.11. To create the directory structure I've used the zf script that comes with the framework. I can use the same script to create controllers/views like so:
/home
/contact
/blog
/about
Yet where I've come unstuck is trying to encompass multiple directories e.g.
/contact/address
/about/cv/workhistory
...I just get an error saying the content cannot be found. There is no model, just a controller (route?) and a view. I've tried adding the desired directory structure when I use zf but this results in an error e.g.
zf create controller about/cv/workhistory
Is this even possible in MVC or does it show a lack of understanding of what MVC is on my part? Thanks in advance for any help/tips/pointers
ok you've created some controllers. Now it looks as though you want to add some functions to your controllers.
In order to do /contact/address it would be appropriate to have the addressAction() inside your ContactController().
A controller is a group of actions (read METHODS). When you built your application 2 controllers were built with it, one of them was the IndexController() that also gave you your first action indexAction() and your first view index.phtml located at /application/views/scripts/index:
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
}
}
Zend Framework implements the MVC paradigm with actual Models, Views and Controllers. In the controllers are your actions grab data and prepare it to be viewed.
Go ahead and do yourself a favor and run through some tutorials, here are some suggestions.
Rob Allens ZF 1.11 tutorial
Zend Framework Quickstart
The naming conventions are important to understand and can be found at:
Zend Framework Naming conventions

Codeigniter HMVC and CMS

I am using Codeigniter with the HMVC Modular extension and have a backend CMS area for managing website content. I am routing the base URL + "admin" (http://localhost/cms/admin) to controller methods with the prefix "admin_".
e.g. function admin_index() {...}
And here is my routing:
$route['admin/([a-zA-Z]+)/(:any)'] = "$1/admin_$2";
$route['^admin/(:any)(/)?'] = "$1/admin_index";
$route['^admin(/)?'] = "dashboard/admin_index";
$route['admin/logout'] = "login/admin_logout";
With the HMVC it is not routing correctly now. Here is what happens:
URL: http://localhost/cms/admin/faqs
Directory: modules/faqs/controllers/faqs - index method
--
here is where it breaks
--
URL: http://localhost/cms/admin/faqs/categories
Directory: modules/faqs/controllers/faqs - categories method (does not exits)
Desired: modules/faqs/controllers/categories - index method
How can I use HMVC while maintaining the "admin" are of the website?
You are making life a bit too tricky by putting frontend and backend functions in the same controllers. Have a look at my article on how to create an admin structure in CodeIgniter.
I'm working on something similar, and implemented a swapping like you did (3rd option) and it worked fine.
I tried to implement a front controller to handle the admin section, and run modules with HMVC modules::run() and buffer the output as I wish, but then I have faced another issue, you will have to change the URI schemes from / to _ or something else, since you wont be able to send module segments as parameter to your controller because CI relies on "/" for it's routing mechanism.
The only way is to emulate the admin section as Phil suggested, but there is another option to still have control over the code implemented by anyone using your CMS.
You could extend CI_Controller (or MX_Controller in case you are using HMVC) and add an Admin_Controller which will handle your logic and control what modules can do.
Have a look at this CodeIgniter Base Classes: Keeping it DRY

Resources