CodeIgniter Noob - How does the index.php file work? - php

Here is my views directory:
- views/
- home.php
- contact.php
- assets/
- css/
- main.css
config.php: (changing this doesn't seem to do anything)
$config['index_page'] = 'index.php';
routes.php:
$route['default_controller'] = "welcome";
When I request www.example.com/index.php, the default page (i.e. home.php) loads and all of the assets work. However, when I request www.example.com/index.php/welcome or www.example.com/index.php/welcome/index, the page loads but the assets don't work. The same also happens if I try to load the page from a link from the home page.
I have no idea what index.php is for. I want to be able to just request www.example.com/welcome/index which will call the welcome.php controller and call the index method. application/index.php looks pretty important for everything to work, do I haven't deleted it, but I don't really want it. What can I do?
Thanks.

I think you're asking 3 different questions here.
Firstly index.php is the main entry point for your codeigniter app. It's very important as all of your routes will go through the index. The reason you can change it in the config is so you can rename it to something other than index.php if your setup requires it
Secondly, just guessing here but I think your assets are being loaded using a relative path; prefix all of your assets with base_url(); e.g
<?=base_url();?>assets/css/style.css
Thirdly, you'll need a htaccess file to hide the index.php (to give you www.example.com/welcome), or the equivalent if you're not using an apache server, which will look like the below (taken from http://www.codeigniter.com/user_guide/general/urls.html:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Enable mode Rewriting on your server LAMP/WAMP and add a .htaccess file with following code
RewriteEngine on
RewriteCond $1 !^(index.php|resources)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
after doing above process you can use your urls without index.php i.e like www.example.com/welcome/index as well as your assets directory www.example.com/assets

Before all learn How MVC framework will work,
www.example.com/controller/function
If you call www.example.com , then default controller will load. You can change this in config.php
Inside the function you can call view ( your actual html viewable scripts ). follow the user guide from https://ellislab.com/codeigniter/user-guide
by default codeignitor url will be like www.example.com/index.php/controller/function
index.php can be removed using .htaccess

Related

MVC pattern - Show only controller name in URL (hide index.php and action)

I'm using a Hosting Linux service provided by Aruba to run my website.
At this moment, my website URL design looks like this:
mysite.it/index.php?controller=&action=
and I want to hide index.php and the action parameter so my uri looks like:
mysite.it/controller
My basic folder structure is:
www.mysite.it/
config/
controller/
lib/
model/
template/
.htaccess
index.php
Is there a solution to achieve this?
Also, I'd need to keep the original document.URL, because for example in some .js there are functions that search for the action parameter in the URL.
Thanks in advance.
You could include the following text in your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Keep in mind that the rewrite engine must be active in your web server config.

Redirect the URL to another, in codeigniter

I have a webpage which was built with core php, now its been updated to codeigniter mvc. Problem which i am facing is, users are trying to enter the old URL path
example: www.website_name/old_path
now since the mvc layout, it wont work and will get 404 error. i want to redirect the clients who enters www.website_name/old_path for the new one
www.website_name/index.php/new_controller/new_view.
Anyone please help me to resolve this issue.
Thank You!
first of all remove index.php config file.
Open config.php and do following replaces
$config['index_page'] = "index.php"
to
$config['index_page'] = ""
Then create HTACCESS file in application/ folder
having this code....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Now index.php is removed from your URl....
STEP 2) open application/config/routes.php file
Now here define your desired web URL
$route['mycontroller/(:any)'] = "mycontroller/user_detail/$1";
So,Your URL Will be ..
www.website_name/new_controller(or xyz)/
Here you can also change the controller name differently using routes So no one can get the exact name of controller/method name....To Do such customization refer this URL
http://www.codeigniter.com/user_guide/general/urls.html
As Sumit mentioned, you should use URL Routing for this.
In the routes file you can create a list of all the redirects that you want like so:
$route['old_path'] = "new_controller/new_view";
You can use 301 redirect for this type of issue.
Put this code in .htaccess
RewriteRule ^www.website_name/old_path www.website_name/index.php/new_controller/new_view [L,R=301]
or use 301 redirect of codigniter http://www.codeigniter.com/user_guide/helpers/url_helper.html
// with 301 redirect
redirect('/article/13', 'location', 301);

Change Particular route in MVC CI

I have below Url in my sample application in PHP MVC CI
http://localhost:1234/Sample/Index.php/User/index
Here User is Controller and index is Action Method
Can I write this url like below ?
http://localhost:1234/Users
After seeing your code , main mistake you have done you have renamed index.php, which is wrong, please revert that index1212.php to index.php
Rest of the thing you can follow other answers.But this is maon, codeigniter will not run without index.php file
Edit
One more error i noticed, in routes.php,
its
$route['Users'] = 'user/';
not $routes
And access your project like this,http://localhost/Sample/Users
And as far as my knowledge, you cannot hide folder name in url.
If you would like to use custom routes. http://www.codeigniter.com/user_guide/general/routing.html
You do not need to include index.php in routes.php
// application > controllers > sample > Users.php filename first letter must be uppercase. Same with class name.
$routes['users'] = 'sample/users/index'; // Lower case preferred.
// When with out function would go straight to index function
$routes['users'] = 'sample/users';
// No Need for index.php
// $routes['Users'] = 'Sample/Index.php/User/index';
Now url will be
http://localhost:1234/project/users
http://localhost/project/users
Or with index.php
http://localhost:1234/project/index.php/users
http://localhost/project/index.php/users
You may need to remove index.php here are some htaccess
https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter
application > config > config.php
$config['index_page'] = '';
htaccess example
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note WAMP users:
If your using WAMP make sure you have rewrite module enabled.
Sample is your project dir so you can not write the route which you want you can write the route as bellow.
http://localhost:1234/Sample/Users
And your route file will look like bellow.
$route['Users'] = 'User/index';

Directories mess with friendly urls (htaccess) handled by a php

I'm new to friendly urls so maybe something basic is missing.
My htaccess contains:
RewriteRule ^([a-zA-Z0-9-/+]+)/?$ /index.php?surf=$1
inside index.php I handle the request like this:
switch ($_GET['surf']) {
case "whatever":
...
So links are like domain.com/home and I include a home.php using index.php for the purpose
Everything works for standard cases, but if someone inputs the name of a directory as parameter like:
domain.com/css
The page gets messed up. The adress bar shows: http://domain.com/css/?surf=css and the content is like the default case in index.php (home), but without styles and such.
I guess it is trying to visit http://domain.com/css/index.php?surf=css after the htaccess and index stuff.
How can I fix this?
*Note that I have some index.php in some other directories that I would like to have access.
add these lines:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/+]+)/?$ /index.php?surf=$1
This prevents redirecting real files and folders.

URL Rewriting in codeigniter rewrite the URL for next page after login

I'm having a hardtime on this part. Well I created a login page using codeigniter wherein the whole path would be http://localhost/CodeIgniter_2.1.3/index.php will be rewritten into http://localhost/login using .htaccess. Well it works just fine but the problem is after I made a successful login and the page redirects to next page using redirect('next_page'); since the URL would be http://localhost/CodeIgniter_2.1.3/index.php/next_page instead of http://localhost/next_page. I also created a rewriterule for that part :
RewriteRule ^events$ /CodeIgniter_2.1.3/index.php/events [L,NC]
and whenever I key in the http://localhost/next_page in the addressbar the page would appear just fine (since I haven't applied any rules on login and on this page.) I also tried using redirect('http://localhost/events'); this also works just fine but still I know this is not the real solution for this. Oh by the way my .htaccess exists on the www folder of wampserver. Any way to solve this?
First of all your domain ROOT folder should be the codeigniter folder
forexample
c:/www/codeigniter_2.1.3/
just move the files from the codeigniter_2.1.3 to your root folder OR make the codeigniter folder ROOT.
Then use just normal .htaccess rewrite as:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
And use the Codeigniter ROUTER to make your pages as you want them
example:
$route['login'] = 'welcome/login';
Will set http://localhost/login page to be loaded from the Welcome controler Login function.
You can set $routes at your routes.php in the config folder.
P.S> If you want not to see index.php on each page check your Config/config.php for the index_page and make it blank.
You might just want to add virtual host to your localhost. here is one step by step explanation on how to do that.
now all you have to do is create an .htaccess file inside your CodeIgniter_2.1.3 folder that will remove only the index.php in the url.

Categories