Basic way to do user authentication for codeigniter 3 - codeigniter

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.

Related

which is the best way for adding maintenance mode in laravel?

i want to add maintenance mode to my cms which i built with laravel,
i want to add front part of it, but i don't know which is the best place for checking maintenance mode, in my basic layout file or in a middleware or somewhere else.
consider it that this check should be run on each request on my frontend routes, therefor it needed to be super optimized.
please remind that i want to enable this capability on admin panel of cms, so the admin user doesn't has permission on built in laravel maintenance mode.
and i want it to be okay with a shared host environment. (i have no access to shell commands)
any idea?
i tried to put it on my blade file and in a middleware, but i think it's not optimized.
You could add a Middleware to the Front end routes only. When someone enables maintenance mode in the admin area the middleware could check if maintenance mode is enabled and redirect or show a view
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class MaintenanceMode
{
public function handle($request, Closure $next)
{
if (/** Logic Here **/) {
// Show a diff page
}
return $next($request);
}
}

Codeigniter 2 profiling on all pages

I want to add profiling info on all my page is a specific admin user is logged in. This way it's easy to troubleshoot stuff.
I know you can add
$this->output->enable_profiler(TRUE);
to a controller. Is there a way to enable & display profiling on every page, without adding the above to every single function in each controller?
Thanks!
You create file in core folder my_controller.php
MY_Controller extends CI_Controller
call this MY_Controller to all controller with extend MY_Controller instead of CI_Controller

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

Separate Admin and Front in 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

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