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
Related
I've been researching up on how to add views and I'm stumped. I want to add a view and all I get are 404 errors. All the examples I see on the web are just to add a default controller. I have a default controller, now I want to add a new page passed an ID in the URL.
This is the controller xyz.php:
class Xyz extends CI_Controller {
public function index() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_main_view');
}
public function activity() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_activity_view');
}
}
Model is xyz_model.php, and views are xyz_main_view.php and xyz_activity_view.php.
This is routes.php:
$route['default_controller'] = 'xyz'; // works okay
// $route['xyz'] = 'xyz/activity'; // 404
// $route['activity'] = 'xyz/activity'; // 404
// $route['xyz/activity'] = 'xyz/activity'; // 404
// ... many, many other different approaches
I'm able to use http://localhost, but I'd like to use the following:
// map to main view
http://localhost/index
http://localhost/xyz
http://localhost/xyz/index
// map to activity view
http://localhost/activity
http://localhost/xyz/activity
My understanding is that some of the URLs for the main view should work automatically, not seeing it. Just http://localhost.
I haven't even touched how to get an ID from the URL for the activity page. Just want to get over this first hurdle.
Keep this code in routes
$route['default_controller'] = 'xyz';
Then Try this URL to execute "activity()" function in xyz controller.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity
To Pass Parameters such as id's you can use this url.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity/[ID]
For more information please check codeigniter routing library. It is really easy to understand.
https://www.codeigniter.com/user_guide/general/routing.html
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 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.
I have controller named “profile” and the URL for user profile is
www.example.com/profile/user
I am already using the rerouting of codeigniter in the routes file
$route['profile/(:any)'] = "profile/index";
what I am looking is to remove the profile controller name from the URL so it will be SEO and user friendly.
e.g
www.example.com/user
any suggestions?
You may try any one of these
// url could be yourdomain/imran
$route['(:any)'] = 'profile/index/$1';
// url could be yourdomain/10
$route['(:num)'] = 'profile/index/$1';
// url could be yourdomain/imran10
$route['([a-zA-Z0-9]+)'] = "profile/index/$1";
Your class may look like this
class Profile extends CI_Controller {
public function index($id)
{
// $id is your param
}
}
Update : (Be careful)
Remember that, if you have a class Someclass and you use url like yourdomain/Someclass then this will be routed to profile/index/$1 if you have $route['(:any)'] or $route['([a-zA-Z0-9]+)'].
You can use this in a route file:
$route['(:any)'] = "controller_name/$1";
$route['(:any)/(:any)'] = "controller_name/$1/$1";
$route['(:any)/(:any)/(:any)'] = "controller_name/$1/$1/$1";
Try this:
$route['user'] = 'profile/user';
Dont know any generic way to do it
Possible duplicate of How to hide controller name in the url in CodeIgniter?
If I understand your question correctly, you just want to add a catchall to the very end of your routes file. Like:
$route['(:any)'] = 'profile/index';
This will catch everything that isn't caught by another route, so make sure this script contains logic to determine when a 404 should be presented.
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 :)