I have a Codeigniter Project where i add this to my routes.php File
Routes.php
$route['default_controller'] = "site/landing";
$route['404_override'] = 'site/landing/page_not_found';
My directory structure is as
controllers --- site -- landing.php
Landing.php
class Landing extends MY_Controller {
public function index(){
# Some Code Here
}
public function page_not_found(){
#load 404 page Here..
}
}
My_Controlelr.php
class MY_Controller extends CI_Controller {
#Some COde Here..
}
Now when i go to some non existing URL like mydomain.com/ajsdhajksbcasbkjakjghdakjsdh
I get this error
Please make sure the controller specified in your Routes.php file is
valid.
Related
I put the line
$route['404_override'] = 'my404';
in the routes.php file, and I made a controller with a name my404:
<?php
include 'page.php';
class My404 extends Page {
private $page;
public function __construct() {
$page = new stdClass();
parent::__construct();
}
public function index() {
// $this->page->page_title = "Page not found!";
$this->page_view($this->load->view('my404', $this->page, true));
}
}
?>
and I made a View with a name of my404
It is working fine when I make a syntax error in the controller name, but it won't work if I write a wrong method and a Server error will occur instead of the customized 404 page, have I missed something in the routes.php file ?
From Codeigniter user guide:
This route indicates which controller class should be loaded if the requested controller is not found.
Apparently that rout will only work when controller isn't found.
you can instead override the default error_404 page at 'application/errors/error_404.php'.
I have two modules in codeigniter hmvc.
1- Acess
2- Display
Here is My Access Module Controller
class Access extends MX_Controller
{
public function __contstruct()
{
parent::__construct();
$this->load->module('display');
}
public function index()
{
echo modules::run('display/login');
}
public function logout()
{
//$this->load->view('login');
echo modules::run('display/test');
}
}
and here is my display module controlelr
class Display extends MX_Controller
{
public function login()
{
$this->load->view('header');
$this->load->view('login'); // This file resides in Access module view folder
$this->load->view('footer');
}
}
So, When acess controller comes in contact, technically it should access display module login function in controller which in return should display the login form along with the header and footer.
Here the problem is that the login.php is placed in access module view file which is being accessed from display module controller. So, I guess the question is pretty much clear for every one.
When loading the view you just need to add the module name before the view name and it will work. So $this->load->view('login'); will become $this->load->view('access/login');
That should work.
I am trying to learn more about CI. Yesterday I tried to implement MY_Controller.php. I read the instructions in user guide But I could not get what is the advantage of it? And one more thing I did not understand the idea use it.
I have written application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
}
This my home controller application/controllers/home.php
class Home extends MY_Controller {
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page)
}
}
and my view is in in my application/views/pages/home.php.
config/routes.php:
$route['default_controller'] = 'home/view';
$route['(:any)'] = 'home/view/$1';
Now I get 404 error. My questions are:
1) why do I get 404 error?
2) if I add the about page, am I supposed to add a new controller or use the home controller?
MY_Controller Controller put into the application/core folder
Then use
MY_Controller folder path application/core/MY_Controller.php
**Home Controller ** folder path application/controllers/Home.php
class Home extends MY_Controller
to
class Home extends CI_Controller
The reason you're getting a 404 is, you did not have an index function. CodeIgniter by default loads the index function when a controller is called.
It doesn't really matter, but I would suggest you to open a new controller.
It is now working. I fixed by a clean directory structure. But i cant manage to see template/header and footer in the source code.
i try to use custom 404 page from this tutorial http://maestric.com/doc/php/codeigniter_404
my controller error.php :
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
echo "error bro";
}
}
when i open link localhost/mading/admin/blablabla , where there is not function blablabla() in controoler admin. the output : “error bro”
i try change line in method error_404() become the code below,
class Error extends Controller{
function error_404()
{
$CI =& get_instance();
$CI->output->set_status_header('404');
$data['title'] = "404 Page Not Found";
$data['body'] = $CI->load->view('web/404','',true);
$CI->load->view('web/web_page',$data);
}
}
but, when i open link localhost/mading/admin/blablabla , where there is not function blablabla in controller admin. the output : blank page.
the function error_404 not load the view . why the 404 page not load when i open controller admin/blablabla ??
thanks
Reading the comments of that tutorial, many people are having the same problem with that code. Did you change parent::CI_Router() on line 10 to parent::__construct()?
But why not just set the $route['404_override'] variable?
Just set a proper route in your config/routes.php:
$route['404_override'] = 'your_controller/show_404';
And the corresponding controller would look like that:
class Your_controller extends CI_Controller {
function __construct() {
parent::__construct();
}
function show_404() {
$this->output->set_header("HTTP/1.1 404 Not Found");
$this->load->view ('common/errors/404'); //wherever your view is
}
}
That should do the trick for you.
I need to create codeigniter base controller to check allowed ip address in database by mobel function if the ip is exists then user should go to home page but if the ip address is not exists and show 404 page in codeigniter, i can't find core folder in application folder
First, you need to extend a core class, call it MY_Controller.php
Save that file in: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('ip_table_model');
$this->load->library('input');
// assuming there's a function called "check_ip($ip_address)" in ip_table_model
if (!$this->ip_table_model->check_ip($this->input->ip_address()) {
redirect('error_404');
}
}
}
Now, we're assuming you have a model called ip_table_model which connects to database with list of IP addresses, and there's a function called check_ip which will validate whether user has access or not. This is relatively simple, and I won't show any examples on this.
The redirect('error_404'); page does not yet exist, you need to create a controller which shows your 404 page.
Now, for any other controllers in your project, instead of extends CI_Controller, make them extend MY_Controller instead.
Here's an example:
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
Explanation: We're extending CI_Controller to create our own core controller, called MY_Controller. Inside, we're checking if user has access or not through the constructor, which will be called in every other controller in the project.
References:
http://codeigniter.com/user_guide/general/core_classes.html
http://codeigniter.com/user_guide/libraries/input.html
Answer is here (section Extending Core Class).
1.7.2 has a different structure to 2.0.*, therefore there is no core folder in application
In Core Create a new Class .
Name MY_Controller.php
class MY_Controller extends CI_Controller {
// Write your functions here which you wanna use throughout the website
public function abc (){
echo "Helllo";
}
}
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function your_custom_fuctions()
{
$this->abc(); //echo Hello...
//Anything you want to do
}
}
function admin_view($view_name = "", $header_info = NULL, $sidebar_info=NULL,$page_info = NULL, $footer_info = NULL, $data_info = ""){
$this->load->view('Admin/includes/header', $header_info);
$this->load->view('Admin/includes/Left_sidebar', $sidebar_info);
$this->load->view($view_name, $page_info);
$this->load->view('common/footer', $footer_info);
}