how to set homepage in Codeigniter? - php

I organized my controllers and views into subdirectories like this
frontend
admin
I created a Home.php in controllers/frontend
and a
home.php file in views/frontend
CI is installed in http://localhost/ci
when I access http://localhost/ci, I'd like the home view to load, but currently I can only access it via http://localhost:8080/ci/frontend/home
I'm guessing it may have something to do with my .htaccess file which is this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I also created the following constant:
define('BASE_URL', '/');
In the routes.php the default controller is set to:
$route['default_controller'] = 'frontend/home';
In the config.php, the base URL is set to:
$config['base_url'] = BASE_URL;
What am I doing wrong?
Thanks

Out of the Box CI 3.x.x doesn't support what you want to do. It's strictly controller/method...
But a little digging found this - Extending the Router Class to support subfolders
This allows you to override the behaviour of the router class and use sub folders.

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.

CodeIgniter URI Routing - How to remove index.php

Here I am with a question that might have been heard many times but still I could not find a perfect solution for that.
My question is that : How to hide the module name , controller name and method name from URL in php codeigniter.
Yes I have googled a lot and have seen many of the videos but did not reach to a success.
Below are the lines of code of different files:
<li>Home</li>
In above code there is a ul li menu bar in which when I click on Home it should redirect to the homeIndex method of UserController and user module.
UserController is the Controller name
homeIndex is the mentho in UserController
and user is the module. I am following HMVC structure here.
Currently when I click on home it creates below url
http://localhost/KWeds_svn/kweds_hmvc/index.php/user/UserController/homeIndex
Below is my .htacces file code that I am currently using
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Below is the base_url defined in config.php
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
$config['index_page'] = 'index.php';
My expected output for the url is only
http://localhost/KWeds_svn/kweds_hmvc/
Please let me know how can I hide module name ,controller name and method name.
There is a simple solution to using URI routing in CodeIgniter.
If you open the routes.php file in the config folder you can map URI's to controllers and methods.
In your case, it would work something like this:
$route['KWeds_svn/kweds_hmvc'] = 'UserController/homeIndex';
Also you need to figure out how to remove the index.php from the URI, to do this, use the following steps:
Go to config.php and change
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
Then go into the document root folder (where the config, application folders are held) and create a new file called .htaccess, in this file write the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
If you are hosting your own server, you may need to enable the rewrite module in apache. To do this, go to the command line for your server and type the following:
sudo a2enmod rewrite
Hope this helps!

Why is my main controller not working in codeigniter

I am brand new to codeigniter but I have been a PHP developer for years.
A client of mine has a exciting codeigniter site that was developed my another developer and I'm trying to set up a dev server to work on the site.
I can load the index.php in views if i go to this url
http://102.236.250.217/app/views/nycity/
But none of the header/footers or css are working
I feel like my controller "class Nycity extends MY_Controller" is not working what could be the cause of this?
This is my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
Read codeigniter's user guide, learn directory structure, it's MVC. Find where your index.php file is and point domain there.
The rest is magic. Good luck!
P.s. MY_Controller should be located inside application/core according to the rules.
First, you have to check your route default controller, you should find routes.php in config folder, and this what you should write in:
$route['default_controller'] = 'your_default_controller';
Depending on your description, it should be like this
$route['default_controller'] = 'app';
And in your app controller should load the "nycity" view

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';

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

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

Categories