Codeigniter with single controller error 404 - php

I'm quite a rookie with CodeIgniter, and as per title, I have troubles trying to setup a single controller for my application. It's a very simple static site with couple of pages like "home", "about" and so on...
I have this in my routes.php file:
$route['default_controller'] = "mycontroller";
$route['404_override'] = '';
$route['(:any)'] = "mycontroller/$1";
And this in mycontroller.php file:
// Home
public function index()
{
$data['page'] = 'home';
$this->load->view('template',$data);
}
// about
public function about()
{
$data['page'] = 'about';
$data['title'] = 'About Us';
$this->load->view('template',$data);
}
I'm working in a localhost environment, and the CI project is in this folder:
http://localhost/local/project/ci-tbs/
and I've specified it also in the config.php file for the base_url parameter.
Now what I'd expect pointing the browser to
http://localhost/local/project/ci-tbs/about
is to find the "About Us" page, instead I got a 404 error. Pointing to the base address corectly gives me the "Home" page.
What am I doing wrong?
Is it sensed to use a single controller istead of 1 per page? I'd totally do that in a quick way to fix, still I'm quite baffled by the fact that I can't understand what I am doing wrong and why it's not working. I'd like to simple set everything in one controller, one method per page.
I've already seen this topic asked here in SO, like using regular expressions in the route $route['(.*)'] = "mycontroller/$1";, but nothing really worked for my case wich I think is quite basic (so basic I'm sure my error is so gross that it will be quite embarrassing :P ).
Additional info:
I have in the folder an .htaccess file picked as is from the Html5 Boilerplate, tried with and without it but 404 is always there. I'm using XAMPP as local environment.
For answer
As mentioned by #Vincent Decaux in the answer, the deal to fix this was to add index.php in the url, the other interesting part is
Create your .htaccess file to "hide" index.php
This way I've resolved another small issue for the pages with missing findings for the assets files, so I used the following rule in the .htaccess file, redirecting all requests to the index.php file and excluding files in assets folder and images, along with robots.txt as suggested here https://stackoverflow.com/a/11846150/1262357
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
hope this helps others with same problems I had!

As mentionned in my comment, it seems to work using :
localhost/local/project/ci-tbs/index.php/about
Create your .htaccess file to "hide" index.php.

Related

Code Igniter Cannot access page via direct link

I am creating a project with Code Igniter as a back end framework and Bootstrap 3 as a front end framework.
I'm having an issue with accessing my pages via directly calling the controller followed by the method.
For example my controller is site.php and the method is home.
Here is what is looks like.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function index(){
$this->home();
}
public function home(){
$data["title"] ="SmartAgent";
$this->load->view("site_header");
$this->load->view("content_home", $data);
$this->load->view("site_footer");
}
As I understand the method index basically sets the method home as the index page.
When I type the web address in my url such as:
examplesite.co.uk
The controller correctly loads my view for the home method, which is content_home.php and the site loads the homepage along with the title fine.
However if I type:
examplesite.co.uk/site/home
This does not work! And I do not know why, this is further causing me issues such as URL's not working etc etc. However base url is set, and I can load CSS, JS, and image files fine. Also I have enabled helpers, routes and all else.
The above url works to load another project I was working on. So is why I know I'm missing something.
Any ideas anyone?
Thanks
Codeigniter routing is done relative to the index.php.
So your link should be examplesite.co.uk/index.php/site/home.
If that is the issue, then you need an .htaccess file, and in it write
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
(If I am not mistaken, writing from my phone)
Then, you will remove index.php from your site.
With codeigniter, you need to set $route[]. This is in application/config/routes.php
Also check out the codeigniter documentation on this, its pretty good and will explain it all.
Thank you everyone for your help and answers!
This was a silly mistake of mine and I wanted to let everyone know so that if anyone was to face this issue this may help. #Alexey answer above gave me a light bulb moment! So thank you.
Basically within the .htaccess mod rewrite file which can be downloaded from google. I forgot to change the directory for my server which is located at the top on line 4
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yourfolderdirectoryhere/
If you are unsure or unclear please watch below tutorial which helped me.
https://www.youtube.com/watch?v=dynPx1B0jis

home.php not being accessed in codeigniter

I have a few sites set up on CI but latest one isn’t cooperating: browsing the domain doesn’t trigger home.php. I get “No input file specified”. I must be missing? (btw, when I put a test index.php file in root the echo code does render)
Here's what I've got set up -
//permissions:
//all subdirectories & file set to 755
//config.php:
$config[‘base_url’] = ‘http://example.com’;
$config[‘index_page’] = ‘home’;
//htaccess in root:
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ /index.php/$1 [L]
//routes.php:
$route[‘default_controller’] = “home”;
$route[‘404_override’] = ‘’;
//home.php:
class Home extends CI_Controller {
public function index()
{
$this->load->library('session');
$this->load->view('main_view');
}
}
My settings seem to mirror those of my other the working CI sites but I’m definitely missing something. Would appreciate feedback on what else I should look for. Thanks
Since you're removing index.php from your URI, you don't need to specify an index_page in config.php. Setting $config['index_page'] = ''; should solve your problem. You should be fine leaving your $route['default_controller'] as it is, since CodeIgniter automatically defaults to the index method.
Your 'default_controller' config must be 'home/index'
Also be sure you placed the default config on the bottom of the file. In the CI routes files, always start from the most specific to the most general ...

codeigniter only pages to an existing website

My website structure somewhat looks like below
css/
lib/
js/
index.php
profile.php
products.php
checkout.php
orders.php
invoice.php
I have added a codeigniter folder in there ...
codeigniter/application/
codeigniter/application/controllers/
codeigniter/application/controllers/mycontroller.php
and other files
I can access CodeIgniter stuff by going to mywebsite.com/codeigniter/mycontroller etc fine.
However, I want to get rid of /codeigniter/ part from the URL. So I was wondering if it is possible to create a whitelist of the files which are CodeIgniter specific? For example, if the URL is mywebsite.com/mycontroller then it does CI stuff otherwise it looks for the plain PHP code file. I have only a couple of CI controllers and loads other non-CI files.
Any ideas?
I think you could use .htaccess to rewrite URL's that don't contain .php, css, lib and js. Something like:
RewriteCond %{REQUEST_URI} !^(\.php|css|js|lib)$
RewriteRule (.*) codeigniter/index.php/$1
So:
http://example.com/css/test.css
stays
http://example.com/css/test.css
(as will all requests to css|lib|js. You can append more things here for the rewrite to ignore)
http://example.com/controller/method
becomes
http://example.com/codeigniter/index.php/controller/method
You can test it out here: http://htaccess.madewithlove.be/
More on rewriting: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Short-term Solution
You can start by simply converting the index.php file into a controller and name it whatever you wish:
<?php
class New_default_controller extends CI_Controller {
public function index()
{
// home page stuff here
}
}
Alter the route.php file and set your default controller so that simply visiting your site will trigger the proper controller:
$route['default_controller'] = 'new_default_controller';
Apply the instructions for Removing the index.php file
Now calls to www.mysite.com/profile.php will access the profile.php at your root and calls to www.mysite.com/new_future_page will call your new_future_page controller.
Please let me know if any of this is confusing or you get stuck.
Optimal Solution
I wanted to leave a comment above but this would have been impossible to show as a comment.
You will have to take your PHP files and put them in the controllers folder like this:
codeigniter/application/controllers/profile.php
codeigniter/application/controllers/products.php
codeigniter/application/controllers/checkout.php
codeigniter/application/controllers/orders.php
codeigniter/application/controllers/invoice.php
Please go through and do the Tutorial before continuing any further. Specifically the Static Pages section will help you in achieving your goal.
You will have to convert your current PHP files to follow the flow of CodeIgniter

Why aren't my routes working in CodeIgniter?

I have already read a bunch of the articles on stackoverflow about this topic, such as:
CodeIgniter: SEO friendly URLs
Codeigniter routes not working sometimes
And I swear I have set up everything correctly, but after I put the route in and save my app, and attempt to go to the new URL, or even the old one, they both give me a 404 error.
I have an extension that currently looks like this:
search/map_view/county
that I want to look like this:
map/county
I wrote the following reroute in the routes.php file, which gives me the 404 error:
$route['search/map_view/(:any)'] = 'map/$1';
And just in case I was doing it backwards, I also tried it like this:
$route['map/(:any)'] = 'search/map_view/$1';
That didn't do anything, so I've deduced i did that incorrectly. A thing of note is that I do have apache's mod_rewrite changing my url's to drop the index.php from it. Don't know how that's helpful, but I've noticed it a lot in the other posts.
Am I supposed to change something somewhere else for this? I'm assuming that if I type in the previous address, I should get automatically rerouted to the new one? Or if I type in the new address, it should work automatically? I don't know, it's getting really annoying...
Anyhow, I have a lot of questions about this stuff, but I'm going to start here and then see if I can find the rest of the answers here after I fix this one.
EDIT - I've been asked to include more info. Here it is.
Here's the .htaccess content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Currently I don't have any custom routes defined in the routes.php file, just because I can't get it to work correctly.
The current controller is "Search", with the method "map_view" being passed a variable "county". So the url would be
http://www.base_url.com/search/map_view/county
I want to change this to
http://www.base_url.com/map/county
Everything else I've previously written still applies. Thanks again!
You want your url looks like map/country .
In your routes.php
$route['map/(:any)'] = 'search/map_view/$1';
$route['map'] = 'search/map_view';
And be sure your controller name is Search.php.Also class name is Search that extends CI_Controller and method name map_view() (must be public function)
Look CI Controller Guide for detailed information

How to solve the case when users surf to index.php

In Zend framework, using the MVC, if A user surf explicitly to http://base/url/index.php instead of just http://base/url, The system thinks the real base url is http://base/url/index.php/ and according to that calculates all the URLs in the system.
So, if I have a controller XXX and action YYY The link will be
http://base/url/index.php/XXX/YYY which is of course wrong.
I am currently solving this by adding a line at index.php:
$_SERVER["REQUEST_URI"]=str_replace('index.php','',$_SERVER["REQUEST_URI"]);
I am wondering if there is a built-in way in ZF to solve this.
You can do it with ZF by using Zend_Controller_Router_Route_Static (phew!), example:
Read the manual page linked above, there are some pretty good examples to be found.
$route = new Zend_Controller_Router_Route_Static(
'index.php',
array('controller' => 'index', 'action' => 'index')
);
$router->addRoute('index', $route);
Can't say I totally disagree with your approach. That said, others may well point out 5000 or so disadvantages. Good luck with that.
Well it really depends on how you want to solve this. As you know the Zend Frameworks build on the front controller pattern, where each request that does not explicitly reference a file in the /public directory is redirected to index.php. So you could basically solve this in a number of ways:
Edit the .htaccess file (or server configuration directive) to rewrite the request to the desired request:
RewriteRule (.*index.php) /error/forbidden?req=$1 // Rewrite to the forbidden action of the error controller.
RewriteRule index.php /index // Rewrite the request to the main controller and action
Add a static route in your bootstrapper as suggested by karim79.
Use mod_rewrite. Something like this should do it:
RewriteRule ^index.php/(.*)$ /$1 [r=301,L]
I don't think you should use a route to do this.
It's kind of a generic problem which shouldn't be solved by this way.
You better should have to do it in your .htaccess, which will offer you a better & easier way to redirect the user to where you want, like to an error page, or to the index.
Here is the documentation page for the mod_rewrite
I've never faced this problem using Zend Framework. just do not link to index.php file. that's it. and when your are giving your application's address to users, just tell them to go to http://base/url/
when the user enters http://base/url/ her request URI is base/url and your .htaccess file routs the request to index.php, but the request IS base/url. you do not need to remove 'index.php' from the request. because it is not there.
when you are trying to generate URLs for links and forms and ..., use the built-in url() view helper to generate your links. like this:
// in some view script
<a href="<?php
echo $this->url( array('controller'=>'targetController','action'=>'targetAction') );
?>" >click</a>
do not worry about the link. Zend will generate a URL for you.
The way I look at this is that if I have a website powered by PHP and a user goes to http://site/index.aspx then I would send a 404.
Even though index.php does exist in theory, it's not a valid URL in my application so I would send a 404 in this case too.

Categories