I am trying to create custom routes for my CodeIgniter site. Even the most basic routes do not work. For example I have the welcome controller mapped to "test" and it just 404's on me. I am running on MAMP with mod_rewrite enabled.
I have the index.php line in config.php empty..
$config['index_page'] = '';
Here is my .htacess file..
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
And here is my route..
$route['welcome'] = 'test';
In a route, the array key contains the URI to be matched, while the
array value contains the destination it should be re-routed to.
-- CI Documentation
Your physical controller name is welcome. So if you want a URL containing the word test in the first segment be remapped to the welcome class, you should do this:
$route['test'] = "welcome/METHOD";
Where METHOD is method of welcome class.
Note: If class/method was welcome/index, you do NOT need to append /index.
If I read this correctly, that route will try to redirect the controller 'welcome' to the controller 'test'. If you have a controller named 'test' and a function named 'index', you can route the following:
route['welcome/index'] = 'test/index';
Is that what you are trying to do?
A couple things:
Routes in CI are cascaded, they're evaluated top to bottom, and the router stops at the first match. Make sure, then, to have any custom route placed below the 2 default routes in a vanilla distribution:
$route['default_controller'] = "welcome";
$route['404_override'] = '';
// custom routes here
$route['welcome'] = "welcome/test";
If you have a controller named "welcome" (the default one), and you want to call a method named "test", you need a route like
$route['welcome'] = "welcome/test"
which will be accessible at the url http://wwww.yourdomain.com/welcome
(if no route were specified, you would have accessed it like http://www.yourdomain.com/welcome/test)
Usually, controller have an index method which is called automatically when no other method is provided. The route you've created so far isn't working because it's calling the index() method of a "test" controller, which is likely not present.
A suggestion: if you mainatain the "welcome" controller as the default one, and you want to call an url like http://www.yourdomain.com/test
You need a test() method and your route must be
$route['test'] = "welcome/test";
Related
I am creating a web application in Codeignitor using Laragon as my local server. When I try to "redirect" to a Controller - I get "404 Page Not Found". If I redirect to View - it works. I can access Controllers with other methods such as "Form Open".
Here is my .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This is my Controller - for a test I used Redirect to a View "page-login" and a Controller "Private-area". I can access the View, but the Controller sends to 404 Page Not Found.
if($this->form_validation->run()){
$result = $this->login_model->can_login($this->input->post('user_email'), $this->input->post('user_password'));
if($result == ''){
redirect('private_area');
}
else {
$this->session->set_flashdata('message', $result);
redirect('page-login');
FYI I can access Controllers (in this example "Register") using other methods such as Form Open like this:
<?php echo form_open('register/validation'); ?>
Why do I get the 404 error?
Your redirect syntax is incorrect:
the CI function redirect() is structured like this:
redirect($uri = '', $method = 'auto', $code = NULL)
keep in mind to use a relative path like '/my_controller/my_function'
as in this example:
redirect('/login/form/');
you need to autoload/load the URL helper with: $this->load->helper('url');
the form_open() syntax is correct, you need to autoload/load the Form Helper with: $this->load->helper('form');
redirect() function
Does a “header redirect” to the URI specified. If you specify the full site URL that link will be built, but for local links simply providing the URI segments to the controller you want to direct to will create the link. The function will build the URL based on your config file values. (- source)
So, the url must be
redirect("/controller/method/parameters");
Or full url
In your code, Codeigniter will look for the index() method of private_area controller.
Thanks All!
So I think #Don'tPanic nailed it. I thought "Redirect" would point to a Controller - but it points to a Route. So I created a Route in my Routes.php file where
$route['private_area'] = 'private_area';
And everything works. Is this the correct way to do this?
Ie to define Routes in the Routes.php file...then call upon them as required using "Redirect"?
I was using small letter for controller file name before,
however, recently move the file to other server that need to controller name to be start with capital
=========================================
Here is the rename example :
file name: Home.php
$route['default_controller']: "home";
link: http://example.com/index_folder/
==========================================
it show error of
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.
And when I change the route to
$route['default_controller']: "Home";
it still show the same error,
Only success if I go to
http://example.com/index_folder/Home/
How to fix that? Thanks for helping
Update
Here is the htaccess file, at the root of the project folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
The reason of rename is it seems the new server has problem if my Controller is in lower case
Linux is case sensitive, keep the file name and class name same and try again. It will work.
The same code will work fine on windows but give exception on Linux.
As per Documentation,
class Home extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
Then save the file to your application/controllers/ directory.
Important:-
The file must be called ‘Home.php’, with a capital ‘H’.
Now visit the your site using a URL similar to this:
example.com/index.php/home/
CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested.
To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'home';
Where ‘home’ is the name of the controller class you want used.
If you now load your main index.php file without specifying any URI segments you’ll see your “Hello World” message by default.
This is the error that i am getting.
Please make sure the controller specified in your Routes.php file is valid.
Let me explain this.
In my application i have used HMVC. In my application folder there are two folders back-modules and front-modules.in my front-modules folder there is a folder called "home" and it has controllers,modules and views.
controllers->home.php
this is my home.php
class Home extends MX_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array('form'));
}
public function index(){
$this->template->build('home');
}
}
and this is my routes.php
$route['default_controller'] = "home";
$route['404_override'] = '';
when i try to go to the site, it gives this error
unable to load your default controller on Codeigniter.Please make sure the controller specified in your Routes.php file is valid.
this system works fine on localhost.
Someone please help me in this matter.
Typically there is a one-to-one relationship between a URL string and its corresponding controller class/method. The segments in a URI normally follow this pattern:
example.com/class/function/id/
your 'default_controller' should be:
$route['default_controller'] = "home/index";
read official CodeIgniter URI Routing Documentation
Kindly Check .htaccess file exist or not in root place of your website.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
Some times codeigniter won't find the controller.
Go to config > routers.php
and see the value of default_controller
make a controller in controllers folder with the same value.
According to codegniter manual managing multi application
in your index.php file change
$application_folder = 'application';
to
$application_folder = 'application/home';
and make sure the controller file name starts with capital letter
controllers->Home.php
and keep Routes.php configurations
$route['default_controller'] = "home";
I'm trying to setup a blog script on a website running on the CodeIgniter framework. I want do this without making any major code changes to my existing website's code. I figured that creating a sub domain pointing to another Controller would be the cleanest method of doing this.
The steps that I took to setup my new Blog controller involved:
Creating an A record pointing to my server's ip address.
Adding new rules to CodeIgniter's routes.php file.
Here is what I came up with:
switch ($_SERVER['HTTP_HOST']) {
case 'blog.notedu.mp':
$route['default_controller'] = "blog";
$route['latest'] = "blog/latest";
break;
default:
$route['default_controller'] = "main";
break;
}
This should point blog.notedu.mp and blog.notedu.mp/latest to my blog controller.
Now here is the problem...
Accessing blog.notedu.mp or blog.notedu.mp/index.php/blog/latest works fine, however accessing blog.notedu.mp/latest takes me to a 404 page for some reason...
My .htaccess file looks like this (the default for removing index.php from the url):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
And my Blog controller contains the following code:
class Blog extends CI_Controller {
public function _remap($method){
echo "_remap function called.\n";
echo "The method called was: ".$method;
}
public function index()
{
$this->load->helper('url');
$this->load->helper('../../global/helpers/base');
$this->load->view('blog');
}
public function latest(){
echo "latest working";
}
}
What am I missing out on or doing wrong here? I've been searching for a solution to this problem for days :(
After 4 days of trial and error, I've finally fixed this issue!
Turns out it was a .htaccess problem and the following rules fixed it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks to everyone that read or answered this question.
Does blog.domain.co/blog/latest also show a 404?
maybe you could also take a look at the _remap() function for your default controller.
http://ellislab.com/codeigniter/user-guide/general/controllers.html#default
Basically, CodeIgniter uses the second segment of the URI to determine which function in the controller gets called. You to override this behavior through the use of the _remap() function.
Straight from the user guide,
If your controller contains a function named _remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Hope this helps.
have a "AllowOverride All" in the configuration file of the subdomain in apache?
without it "blog.notedu.mp/index.php/blog/latest" work perfectly, but "blog.notedu.mp/latest" no
$route['latest'] = "index";
means that the URL http://blog.example.com/latest will look for an index() method in an index controller.
You want
$route['latest'] = "blog/latest";
Codeigniter user guide has a clear explanation about routes here
My url link looked like:
http://localhost/CodeIgniter_2.1.2/index.php/pages/home
Then I wrote the .htaccess file using google as follows:
RewriteEngine on
RewriteCond $1 !^(index\.php|themes|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Now my link looks like:
http://localhost/CodeIgniter_2.1.2/pages/home
Now,I want to remove pages from this link. Can anyone help me figure this out?
So, my url could look like:
http://localhost/CodeIgniter_2.1.2/home
One way to do it is to create a custom route in the application/config/routes.php file, which routes the url 'http://localhost/CodeIgniter_2.1.2/home' to 'http://localhost/CodeIgniter_2.1.2/pages/home'
Custom routing is described here
The code will look something like:
$route['home'] = "pages/home";
EDIT
Your controller, method, and variable are named "pages", "view", and "home" respectively. Therefore, you should try the following route instead:
$route['home'] = "pages/view/home";
Also:
** Reserved Routes **
From the codeigniter documentation:
There are two reserved routes:
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the URI contains no data, which will be the case when people load your root URL. In the above example, the "welcome" class would be loaded. You are encouraged to always have a default route otherwise a 404 page will appear by default.
Thus, you should not be setting the default controller to "pages/view/home." Rather, you should create an "index" method in the controller that defaults to the "home" view.
Also
Don't forget to change the $config['index_page'] from 'index.php' to '' in the config.php file located in application/config/config.php.
Try changing your rule so that the target is:
RewriteRule ^(.*)$ index.php/pages/$1 [L]