Getting 404 using Codeigniter 2.1 - HMVC 5.4 - php

This is my first hmvc attempt and it's going wrong... I'm trying to separate my site zones through different modules. My intention then is control the access using ion_auth for hmvc. Thats my initial structure:
/application
/modules <-- hmvc
/public_zone
/controller
public_zone.php
/view
...
/private_zone
...
So, to do this (being installed hmvc on core and third_party folders) I tried to setup as 'default_controller' the 'public_zone' controller. His mission is load the root page ('localhost'), but *it returns a 404 error.*
This is my public_zone.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Public_zone extends MX_Controller {
public function index()
{
$this->load->view('include/header');
$this->load->view('frontpage');
$this->load->view('include/footer');
}
}
Maybe the problem is on routes.php (application/config/routes.php) file? I'll tried this:
$route['default_controller'] = "public_zone";
$route['404_override'] = '';
Anyone can tell me what Im doing wrong? For sure I misunderstood some hmvc concept... But the fact is I don't know why it doesn't works :(

First, it's important that you leave the CI structure intact.
So you need the structure:
/application
/controllers
public_zone.php
/views
frontpage.php
/include
header.php
footer.php
/models
Ofcourse, you need also the other folders that comes with CI.
Second, you need some changes in the controller in order to make it work.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Public_zone extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('include/header');
$this->load->view('frontpage');
$this->load->view('include/footer');
}
}
After this changes it's supposed to work :)
Be also sure to read the user guide: http://codeigniter.com/user_guide/
It might cost you some time to read, but later it will save you a lot of time.
Good luck!

Related

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?

Codeigniter 3 - Make URLs case insensitive

With CI3 released controllers must now be Ucword-style (for whatever reason). No problem changing these, but upgrading any site now leads to 404s wherever it's applicable (which is pretty much everywhere).
Is there a way to make it so the old URLs still work (in addition)? Ie I have a controller 'Admin.php" the index() fn of which used to be called
http://example.com/admin
now it must be called
http://example.com/Admin
Is there a way to have both work (on CentOS). Maybe via Apache rewrite and/or config?
You can set your routes in :
application/config/routes.php
$route['admin'] = 'admin/index';
You can set your Controller as following code in controller.
application/controllers/Admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model(array('admin_model'));
}
public function index()
{
$this->template->view('admin/index');
}
}
?>
If case insensitive routes are required, do below changes to URI.php
Location of File: system/core/URI.php
Find $this->_parse_request_uri() and replace it with strtolower($this->_parse_request_uri())

Separating Controllers for each module in Code Igniter

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

Codeigniter - Extending my own controllers

I'd like to know if it's possible to extend my own controllers. I've been working on web-based applications for a while and I'm now starting to find each customer that wishes to use the application has different requirements of how it should work. My thoughts are that if I generate a base structure and then extend the controllers to override any of the functions that the customers require to work differently.
First of all, could you tell me if I'm on the correct track, and secondly, how do I go about extending my own controllers (if I can)? I've tried the usual:
Class Reports2 extends Reports { }
This doesn't work but I'm guessing it has something to do with the location of the file I'm trying to extend. My file structure is as follows:
Application
--->controllers
-------->control_panel
------------>reports.php
If I am not mistaken then you should be able to easily do this:
reports2.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(APPPATH.'controllers/control_panel/reports.php');
Class Reports2 extends Reports {
public function __construct(){
parent::_construct();
}
public function index(){
}
}

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