Separating Controllers for each module in Code Igniter - php

P.S This question may already be existing but the answer didn't satisfied what I was trying to figure out. I'm using code igniter for the first time in my new project.
So I have this only one controller which is main.php and populated with a lot of public function. Now everytime I go to main.php the url is looks like cmms/main and everytime I'm going for its subclass it goes cmms/main/asset.
Now the subclass asset has many functions which are in main.php. What I want is to make separate controllers for each module. so i have cmms/main as main.php & cmms/asset as asset.php instead of making it under cmms/main/asset. Would this be possible? or I should just leave it alone and continue putting all codes in the main.php controller?
My default route is the main controller.
$route['default_controller'] = 'main';

You have two ways to do that.
Keep one controller but URL is different. (Method 01)
Keep new controller for each new function. (Method 02)
(these topics describe below)
Method 01
go to - config/routes.php, and add new routes like this
$route['cmms/asset'] = 'cmms/main/asset';
$route['cmms/contact'] = 'cmms/main/contact';
So in view you should call anchor tags like this
Assets
Contact Us
Method 02
Create new controller for each new methods.
File name - main.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
}
File name - asset.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Asset extends CI_Controller {
}
File name - contact.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Contact extends CI_Controller {
}
So in view you should call anchor tags like this
Assets
Contact Us

you can create Modules for your class in following ways,
By creating separate class under your application controllers directory eg: main.php, assets.php here your URL will be cmms/main or cmms/asset
Creating sub folders under your application controllers directory and create controller classes under it main/main.php, asset/asset.php here URL will be cmms/main/main, cmms/asset/asset, if you want just cmms/main or cmms/asset in URL than you need to create routes in route.php file.
This way you can make codeigniter Modular in this way every module will have separate controller, model and view even libraries in its own module directory and URL will be same as cmms/main or cmms/asset for each controller. you need to install wiredesignz's HMVC extension here is URL you can download https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/overview
important please read documentation for HMVC extension to use it.

it seems that you are having trouble to write long url each and every time....
So,better way is to use routes for ease.
For Ex: $route['login'] = "main/login";
Now if you use login directly in url it will call main class and it's login method
using routes you can prevent exposing your controller name and also for shorten URl

Related

Change default controller directory of Codeigniter

I want to have two folders where save codeigniter's controllers:
/application/controllers
/application/buckets
i'm a order paranoic person and i want to separate two types of my controllers.
In bucket folders the structure app was this:
/application/buckets/example/index.php
/application/buckets/example2/index.php
/application/buckets/example3/index.php
¿Maybe extending the router class?
A working example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Extended the core Router class to allow for sub-sub-folders in the controllers directory.
*/
class App_Router extends CI_Router {
function __construct()
{
parent::__construct();
}
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
}
if (file_exists(APPPATH.'buckets/'.$segments[0].'/index.php'))
{
$this->set_directory(APPPATH.'buckets/'.$segments[0]);
$this->set_class(ucfirst($segments[0]));
$this->set_method(isset($segments[1]) ? $segments[1] : 'index');
return $segments;
}
}
}
You can use Hierarchical MVC(HMVC) with Codeigniter to accomplish this.
For reference, see Modular Extensions - HMVC
You may want to look into parent-child controller ...one extending another. To be more clear you can make as many controller you want.
I Agreed with #Brian Gottier : "what does changing their location do?"
You can perform anything if you have core functionalities in your hands.
You can play around with hooks (CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page.)
Create "Base"/"Admin"/"Public"/"XYZ" Controllers in
application/core/MY_Controller.php
and keep rest of your controllers in same application/controller folder
MY_Controller is a basic core library extension. Whenever you create a class with the MY_ prefix the CodeIgniter Loader class will load this after loading the core library.
All we have done here is create a base class that all of our Controllers and "controller types" will inherit. Anything we put in here and assign to $this will be available to anything that extends this class.
Base Controllers are a nice simple way to give you global data, logic and shared code which can be specific to a certain part of your site. They can do all sorts of crazy stuff which I will leave for you to think about.
I Hope this help.

Using custom named controllers with codeigniter

Hi I'm working on a application that uses a admin environment and a user environment. To make my code and controllers more readable I would like to use some kind of a prefix for my admin and user controllers. Or maybe use a different directory for the admin en user controllers. Without using the exact name in the url to call the controllers.
Does anybody know if there is a way to do this or a work around for my idea.
For example I would like to use: user.[controllername].php or user_[controllername].php
EDIT:
So when I use routes to separate the user controllers from the admin controllers, it brakes my template resolver.
For instance lets say the url is: http://myapplication.com/user/profile in normal cases this would call the user controller and the function profile. My template resolver looks for the folder user and checks if there is a level1.tpl file.
Lets say we want to edit this profile the url would be: http://myapplication.com/user/profile/edit/1 now it would search in the folder user for a level2.tpl file and would call the function edit with id 1.
The problem with the routes is that it prepends something to the url witch I don't want because then it searches in the wrong folder for instance when we use admin/dashboard it will look in the folder admin instead of the folder dashboard.
So basically what I only need is only to tell the application to use a different directory where the controllers are located or a different filename base on what kind of user is logged in.
Thanks in advance, for sharing you knowledge.
You can do that in many ways.
1. Create two file in application/core named "Admin_Controller.php" and "User_Controller.php" with following class signature,
class Admin_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
and
class User_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
In these classes you can do use your PHP logic to do whatever you wants, just simply extends your Controllers to Admin_Controller or User_Controller.
eg: class Home extends User_Controller {} or class Dashboard extends Admin_Controller {}
2. You can also simply do that with routes.php
eg:
// for admin
$route['admin/(.+)'] = 'admin_$1';
// for frontend
$route['(.+)'] = 'user_$1';
I hope this might give you some idea.

How do I make codeigniter to load a HTML project?

I have a project which is this one:
Its just a html webpage with the features of letting the user modify the product the way they want, it currently does not have any backend.
I have done this:
In my app/controllers I have a made one controller named:desing
it has this
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class desing extends CI_Controller {
public function index()
{
$this->load->add_package_path(APPPATH.'third_party/desing_tool', FALSE);
$this->load->view('index');
}
}
I have the third party project in the folder third_party and my routing settings are this:
$route['desing'] = 'desing';
but it doesnt load, I want to add a shopping cart, I will make a cms but I cant make code igniter load this.
Any Ideas?

understanding the basic codeIgniter

<?php
if (!defined('BASEPATH', ''))
exit('no direct script access allowed');
class Hello extends CI_Controller {
public function index() {
echo "this is my index function";
}
public function one() {
$this->load->view('one');
}
}
?>
I am the new user of codeIgniter.First i download code igniter software,then place it to the xampp/htdocs.Then i create a simple codeIgniter code which are show in the above.I don't understand where this code is to be save.I save this code in the Codeignitor/appilication.but it give me the below answer:
no direct script access allowed
What are the fault.please help me anyone.
Have you read the User Guide of Codeigniter? This is your Controller code and it should be placed inside appilication/controllers folder by name hello.php. See this.
Before starting of any framework it would be good if you understanding directory structure, and what is the role of files.
Where should keep HTML, CSS, JS and logic and model part.
As you creating controller then it should save under
application/controllers/ControllerName.php
Here is the guideline for controller
Also refer tutorials
http://tutorialcodeigniter.com
http://codesamplez.com/development/codeigniter-basic-tutorial
Open config.php within CodeIgniter\system\application\config.
Change base site url http://localhost/foldername

Can't access a Codeigniter controller in sub folder

I have set up a folder with a controller in it: controllers/admin/home.php, but I get a 404 from the browser when I try to access it.
This is my routes file:
$route['employers'] = "employers/home";
//$route['employers/dash'] = "employers/dash";
$route['default_controller'] = "home";
$route['404_override'] = '';
This is the controller file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class home extends CI_Controller {
function __construct(){
parent::__construct();
/*
enable profiler
*/
//$this->output->enable_profiler(TRUE);
$this->load->helper('url');
$this->load->library('ion_auth');
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper('layout');
}
}
.htaccess seems fine standard. Any ideas on what i'm doing wrong?
Note some things:
1) Routes are executed in the order they're written, and your custom routes MUST follow the default ones. So, it should be:
$route['default_controller'] = "home";
$route['404_override'] = '';
$route['employers'] = "employers/home";
This if your controller "home" is inside the folder "employers".
2) Controllers don't need all that stuff you wrote, indeed you don't even need to call the parent constructor unless you're planning to load libraries and resource for the whole controller's methods (which can be achieve also by autoloading them in the autoload.php file), so it could simply be:
file: application/controllers/employers/home.php
class Home extends CI_Controller {
function index()
{
// this is the method you're calling with your URL!
}
}
3) As per above, and as already pointed out by #Wesley, with your url you're trying to access the INDEX method of your controller HOME in your subfolder EMPLOYERS. But you didn't defined an index() method (which is the one called by default if no other is supplied).
It seems, instead, that CI is trying to look for an employers controller and a home method; if it doesnt find it, but you have a employers folder, it tries to access the index method in the home controller in the employers folder. And, since it didn't find it either, you're getting the 404 page.
Hope I'm clear, otherwise just ask.
You failed to say how you were trying to access it via url. It should be:
{YOUR_BASE_URL}admin/home
... followed by optional URL segments (/method/param1/param2/etc).
Without the additional segments, this would by default load the index method. However, since you don't have any methods defined, there's nothing to load.
If this still fails after you define a method, move the controller file out of the sub-directory for starters, and make sure it works.

Categories