I have single controller and one view page when i try to load this view page using controller I am getting 404 error even route has set.
Controller:
class Navigation extends CI_Controller{
public function index(){
$this->load->view('header');
}
}
In the view directory i had place a file with the name of header and it contains as "Test Navigation" in body that's it.
Routing file:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
$route['menu'] = 'Navigation';
This CI script placed under directory of books. So when i type as localhost/books/index.php/menu , I am getting 404 error. Please explain how to solve this.
Controller class name & controller filename should be same. class Navigation should be under Navigation.php.
Note: This rules applied for the Model too.
Related
I have multiple controllers in a Codeigniter project. I need to hide these controller names. For eg: I have two controller, home and school. Each school have their own page which includes about,gallery,contact etc. and url should be http://www.sitename.com/schoolname. I hide home controller using routes.php.
$route['(:any)'] = 'home/$1';
But it shows error in school controller. Please help me..... Thanks.
It is not true that a controller name is absolutely required.
In routes.php first define default controller to be home (when user accesses homepage sitename.com)
$route['default_controller'] = 'home';
Then you create the rule that any other page should be redirected to school controller:
$route['.*'] = 'school';
Now the home.php controller will look like this:
class Home extends CI_Controller {
public function Index()
{
echo "This is the homepage";
}
}
And in the school.php controller you have to manually get the name of the school from requested URL:
class School extends CI_Controller {
public function _remap()
{
echo "User requested school: " . $this->uri->segment(1);
}
}
Why use _remap method? Because it will be called everytime regardless of what's in URL or routing.
From the docs:
If your controller contains a method named _remap(), it will always get called regardless of what your URI contains.
try this
$route['anything you want/(:any)'] = 'home/$1';
controller name is required so set it with any name you want
I'm new to CodeIgniter. I have a homepage with links. I know how to link the homepage with another page, but I think I'm doing something wrong in the routes.php
I looked through the tutorials, but I still can't find the problem. I tried writing the routes in different ways. Can someone help me?
View of home.php
<ul>
<li>Homepagina</li>
<li>Over</li>
<li>Inloggen</li>
<li>Registreren</li>
<li>Profiel</li>
<li>Matches</li>
<li>Config</li>
</ul>
Controller of home.php
class Pages extends CI_Controller {
public function view($home ='home')
{
$this->load->helper('html');
$this->load->helper('url');
if (! file_exists(APPPATH.'views/pages/'.$home.'.php'))
{
show_404();
}
$data['title'] = ucfirst($home);
$this->load->view('templates/header',$data);
$this->load->view('templates/slideshow', $data);
$this->load->view('pages/'.$home, $data);
$this->load->view('templates/footer',$data);
}
}
routes.php
$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';
Controller of Login
class Login extends CI_Controller {
public function view($login ='login')
{
$this->load->helper('html');
$this->load->helper('url');
if (! file_exists(APPPATH.'views/pages/'.$login.'.php'))
{
show_404();
}
$data['title'] = ucfirst($login);
$this->load->view('templates/header',$data);
$this->load->view('pages/'.$login, $data);
$this->load->view('templates/footer',$data);
}
Thank you for the help in advance!
just call controller and method
<li>Inloggen</li>
If not works
<li>Inloggen</li>
try this
When you extend a class that has a constructor, you need to call that constructor from the class you are extending it from... ie
class Controller_name extends CI_Controller {
public function __construct(){
parent::__construct(); // Call the CI_Controller __construct();
}
Note that the __ is actually two underscores but appears a a single line on here.
With your Routes
routes.php
$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';
Remember that a CI URL is of the form domainname.com/controller/method
The default_controller is where the browser will be redirected to if you do not supply the controller/method ie - just domainname.com. With your default controller route, you will be redirecting to your pages controller and accessing the view method. Going by your code that should be fine.
Your login route should also be fine if you access domainname.com/login as your url - this will be sent to your login controller, view method and passing in login as the page name.
home.php
<ul>
<li>Homepagina</li>
<li>Over</li>
<li>Inloggen</li>
<li>Registreren</li>
<li>Profiel</li>
<li>Matches</li>
<li>Config</li>
</ul>
With your link to home.php, you might want to create a controller called Home.php and define the index() method. So change
Homepagina
to
Homepagina
The link then becomes domainname.com/home - which points to your home controller and it's index method.
Then we come to this
<a href="<?php echo site_url('login/login');
This will create domainname.com/login/login. Your login route is only looking for domainname.com/login. So the current link domainname.com/login/login is looking for a Controller called login with a method called login.
Your link with myprofile/myuserprofile will be looking for a controller called myprofile and a method called myuserprofile. There is nothing in your routes.php to deal with this. So if that is not working, it's nothing to do with the routes.
So this is all pointing back to you (possibly) not having defined the __construct methods as I discussed at the beginning.
<li>Inloggen</li>
if it works you can remove index.php.
follow this documentation to remove index page
If you don't use full path of url i.e. starting with http[s]://, it's always smart start links with fore slash:
<li>Over</li>
You can use CI url helper and be more safe in case you are struggling™ with .htaccess and index.php with:
echo base_url('pages/about')
Also, practicing static pages tutorial, pay attention on docs' Loader lybrary page, so you can find how to pass all data to all view pages, leaving it to view files and in that case you don't need to set second parameter in here:
$this->load->vars($data);
$this->load->view('templates/header');
$this->load->view('templates/slideshow');
$this->load->view('pages/'.$home);
$this->load->view('templates/footer');
And all $data array will be passed to all view files and you'll be able to make output of those.
Maybe you find it usefull once. ;)
I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";
i was wondering whats the best way to route to pages in codeigniter? Say for example user wants to route to the index page, but should i create a method in a controller that just fo rthat page, or what is better way?
No need to create separate methods or controllers. Here's how I do it:
class Pages extends CI_Controller {
function _remap($method)
{
is_file(APPPATH.'views/pages/'.$method.'.php') OR show_404();
$this->load->view("pages/$method");
}
}
So the url http://example.com/pages/about would load the view file application/views/pages/about.php. If the file doesn't exist, it shows a 404.
You don't need any special routing to do this, but you can do something like this if you wanted the URL to be http://example.com/about instead:
// Route the "about" page
$route['about'] = "pages/$1";
// Route ALL requests to the static page handler
$route['(:any)'] = "pages/$1";
Routing can be done using the application/config/routes.php file. You can define custom routes redirecting to the index page there. There is absolutly no need to create methods for every page.
A more detailed explanation can be found here: http://codeigniter.com/user_guide/general/routing.html
EDIT:
Didn't realy got what you meant, but here is the solution I use:
class Static_pages extends CI_Controller {
public function show_page($page = 'index')
{
if ( ! file_exists('application/views/static_pages/'.$page.'.php'))
show_404();
$this->load->view('templates/header');
$this->load->view('static_pages/'.$page);
$this->load->view('templates/footer');
}
}
I make 1 controller in application/controllers for the static pages with 1 method in it that I use to load in the static pages.
Then I add this line to application/config/routes.php:
$route['(:any)'] = 'static_pages/show_page/$1';
//you can also change the default_controller to show this static page controller
$route['default_controller'] = 'static_pages/show_page';
In the config file located at /application/config/routes.php
Kind sirs,
I'm using Codeigniter to build a blog. I might need a way to redirect a 404 error into a custom 404 page. Just like what Abduzeedo.com's 404 page. Is it possible to control this by using routes? Or should i use controllers to direct it to another view? Thanks very much!
there is another way which I use: by overriding Exception core class of Codeigniter. Firstly make sure your config file(system/application/config/config.php) subclass prefix is as following
$config['subclass_prefix'] = 'MY_';
Then make a file named MY_Exceptions.php in system/application/libraries. Then override the function show_404() function here as follows.
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
function show_404($page=''){
$this->config =& get_config();
$base_url = $this->config['base_url'];
$_SESSION['error_message'] = 'Error message';
header("location: ".$base_url.'error.html');
exit;
}
}
Now Error controller will be your error page, where the page will be redirected for 404 error.
Custom 404 page is create by using the following 3 steps,
Step 1: First open routes.php in application/config and set a custom controller name,
$route['404_override'] = 'my404'; //my404 is class name.
Step 2: Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here,
<?php
class my404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Step 3: Create a view file with name 'error_404.php' with your custom message.
You don't need to create any controller or modify the code, rather than modify one view file. Just placed your 404 page HTML into the application/views/errors/html/error_404.php file.
If you want to use base_url() or any other CodeIgniter functions, initialize CI_Controller class like the below code.
<?php
$ci = new CI_Controller();
$ci =& get_instance();
$ci->load->helper('url');
?>
For a simple solution, go to your "error_404.php" file in "application/errors/" directory and put this code at the beginning of the file:
header("Location:".base_url());
It'll redirect your 404 page to home page.
Simple Solution.
Create your custom error or Page not found page within view.
Create a controller for error page. Within index function load the view of error page that you already created in previous step.
Now, Go to your application/config/routes.php
you should find $route['404_override'] = '';
Its the '' mean its calling the default error page.
Now place your controller name for error page .
It will work.
Suppose my Controller for error page is 'error'. and view page is 'notfound' .
So, code for Error.php controller will be:
<?php
class Error extends CI_Controller
{
function index()
{
$this->load->view('notfound');
}
}
Now, I replaced $route['404_override'] = ''; to $route['404_override'] = 'error'; in
application/config/routes.php.
Well you want to show your custom 404page when page not found in your website you just replace your custom file on core file which is located in application/error/error_404.php just replace your file here and you are good to go else where you can follow other method by using routes and custom controller for you custom 404 page. But the shortest and easiest way is replacing the core file with custom 404error page file
First open routes.php in the application/config folder and set a custom controller name.
$route['404_override'] = 'custom404'; //custom404 is class name.
Create a new controller and write the following code in it. The code is very easy to understand. So I am not going to explain it here.
<?php
class custom404 extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // View name
$this->load->view('index',$data);//loading in my template
}
}
?>
Create a view file with name 'error_404.php' with your custom message.
That is all you need to do and will be having your custom 404 page now.
No need to define any complexity.
The process is very simple.
First, go to your application->config->routes.php file and change this with your controller's function, where you simply load the header, footer, and 404.php page.
$route['404_override'] = 'welcome/_404';
Then come to your controller's page
function _404(){
$this->load->view("header");
$this->load->view("404");
$this->load->view("footer");
}
The same question is also available here
Display a view for 404 error in CodeIgniter
A more simple solution:
Just edit /application/views/error_404.php with your custom 404 page :)