Code Igniter Cannot access page via direct link - php

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

Related

Codeigniter with single controller error 404

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.

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

Codeigniter URL Dilemma

I am having issues with the urls. I took the .htaccess code from the user guide to remove the index.php from the url and I have removed it from the config as well. Now here is the problem. I have a controller named “main” and a function inside called “join” which simply display the view. Now if I go to http://localhost/myfolder it loads the index view just fine. However if I try to go to http://localhost/myfolder/main/join or http://localhost/myfolder/join it gives me a 404 error. But http://localhost/myfolder/index.php/main/join works however without loading any css from my header file, but the footer is still loaded. Which I am very confused about. How can I possibly fix it so it just works with main/join? I will use routing later to make it just /join however I need the css to load like it does with my “index” view but not with my “join” view. Also localhost/myfolder/main gives me a 404 error as well. All I did was take the code from the user guide and paste it in .htaccess Any help guys?
Question is little confusing but let me try to answer what I got.
Question 1. Make 'localhost/myfolder/main/join' working.
You mainly need to remove index.php. Do the following:
a. (Very important) Make sure apache's rewrite module is enabled. IF you are using wamp, go to 'Apache'>'Apache modules' and make sure 'rewrite_module' is checked.
b. In code igniter, open file application>config>config.php. Search the line:
$config['index_page'] = 'index.php';
Remove index.php from there. Line must be
$config['index_page'] = '';
c. on myfolder, make a file .htaccess and add following code there:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Now URL 'localhost/myfolder/main/join' must be working.
Question 2: Load CSS
To load css, user following code under head section of your view:
<?php echo link_tag('style/style.css');?>
Make sure style/style.css is present in root folder; 'myfolder' in your case.
Hope this helps,
Kapil.

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

Kohana link related questions

So basically, I have two fast questions about kohana urls.
1) With default .htaccess that comes with Kohana, after loading another view, it add's .php to index file. So, for example if I load product view, it looks something like this - http://mysite.com/index.php/products , but I would love it to look http://mysite.com/index/products .
.htaccess code -
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
2) I have two controlles - home and products. Home is default page, but when I load products page, as described above, it shows without design. Both page layouts are same (files are different, I just copied, and I'm testing if it's working).
Controller code -
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Records extends Controller {
public function action_index()
{
$records_view = View::factory('products');
$records = $records_view->render();
$this->response->body($records);
}
}
It loads just a plane html code, I'm not sure if I'm really doing it correctly.
I really hope you will help me find out solution for this, since I checked some Kohana tutorials and it seems I'm doing it fine, also in documentation it's the same thing. I've came to Kohana from CodeIgniter, which was a little bit bigger documentation, and easier to understood, but as far as I found out, many programmers say that Kohana are alot better than CodeIgniter.
Thank you very much for reading this ;)!
2) you should setup your template controller, try this:
Kohana template $content variable shows nothing
And then extends Records from template controller and bind content variable to the view and echo content in template file.
And for the first question, I'd simply advice you to set your index_file to false in init method in bootstrap.php and create index controller if you need it so much.

Categories