Set route in different directories Codeigniter - php

Divided controllers into folders, site in site folder, admin in admin folder (inside controllers).
But I can not access the controllers without putting the base of the same folder url, example:
I want to access like this: http://localhost/gabriel/projeto/about
But just so I can: http://localhost/gabriel/projeto/site/about
File 'htaccess' this so:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
'base_url' this so:
$config['base_url'] = 'http://localhost/gabriel/projeto/';
File 'routes' this so:
$route['default_controller'] = "site/index/";
$route['404_override'] = '';
$route['admin'] = "admin/login";
Already tried modifying the .htaccess but no result.
Inserting another line in the file 'routes':
$route['(:any)'] = "site/$1";
But putting that line it conflicts with the route ends and the Admin can receive only one parameter in Url.
How can I solve this problem? make dynamic route...

I'm not 100% sure this is what you're asking, but try setting the base_url in the config/config.php file
$config['base_url'] = 'http://localhost/gabriel/projeto/';
or set to whatever you want your site root to be

add this in your routes.php.. place it above all other $route variable...
$route['projeto/about'] = 'projeto/site/about';

Solved my problem as well:
$route['admin/(.*)'] = "admin/$1";
$route['(.*)'] = "site/$1";
Now this dynamic route

Related

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!

CodeIgniter Routing Not Working in Sub Directory

I have setup a CodeIgniter site but only the default routing seems to be working.
I have had to set it up as a sub dir of an overall site - so http://localhost/subdir is the default url - this seems to work with the basic out of the box routing.
I have added a Users controller with the following methods - login, logout, register so I was expecting http://localhost/subdir/users/login would work but I get :
The requested URL /subdir/users/login was not found on this server.
Apache/2.4.25 (Win32) OpenSSL/1.0.2k PHP/5.4.44 Server at apps.mediaforce.co.uk Port 80
My routes file is very basic so I tried adding a new route to it for the users controller but it made no difference.
$route['users/login'] = 'subdir/users/login';
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Where am I going wrong?
You don't need to include subdir in your route.
Hence update your $route['users/login'] to route to following:
$route['users/login'] = 'users/login';
Edit 1:
in your root create a .htaccess file if it doesnot exist and save the following content in it to remove index.php from your URL:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

Default Controller of codeigniter shows blank page

here is my url structure-
http://example.com/demo
demo directory contain my all codeigniter application files. I set my base path like this
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";
and also set $config['index_page'] = ''; is blank. My .htaccess file contain-
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [QSA,L]
Suppose i have a controller called Home and contain one method called index. I have set it as my default controller like this on my routes.php
$route['default_controller'] ='home';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
// Catch all route
$route['home'] = 'home/index';
Now the problem is when i am using in my localhost(wamp) it works well. But when I uploaded it to my server and go to this url http://example.com/demo it shows blank page. It should show home controller. I have checked it that if problem present in Home controller. Thats why I go to this url http://example.com/demo/home now its working fine but problem occurred when using the base url.
It's not needed to enter the route for your default controller.
Just remove the line with $route['home'] = 'home/index';; I think it's overriding your default configuration.

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

What is the relationship between .htaccess and routes.php files in CodeIgniter?

This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
And my routes.php file:
$route['default_controller'] = 'pages/view';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
Don't they basically do the same thing? Which is correct to use?
*Edit: My question is not specific for this example. It is more like, which is good for what? It seems like they both to the same thing? Or am I wrong? *
The .htaccess file removes the index.php from the URL, whereas the routes.php file controls where the URI segments are pointing to in terms of controllers, actions, and parameters
in other words,
.htaccess is a directive to the apache server to remove index.php from the requested URL, and routes.php just directs your actual php scripts
The .htaccess file tells apache to match any request to a uri that is not a file or folder in the directory that you are serving and forward it to your index.php controller (also removing the need to reference index.php in the url).
routes.php is a map of uris to controllers, and is a codeigniter specific piece of code. The .htaccess is independent of your codeigniter code, and can be used independently of any php framework

Categories