Change Particular route in MVC CI - php

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

Related

Strange functioning with segments in codeigniter

I am currently working on a system with a particular architecture (not mine). In the main application folder, there is the common asset and 2 others folders (user and admin) with the same kind of asset, which give us this architecture :
application
admin
config
controller
etc ...
user
config
controller
etc ...
controller
etc ...
In the root folder, there is 2 files : user.php and admin.php each having the same code than in the classical index.php except for the line
$application_folder = 'application/admin';
So far, the code is working... But I have found a strange reaction.
In my folder admin, under a controller, I am calling a method, which work except for one thing... the view are not correctly loaded.
In all other method I have this kind of code, with only one parameters :
public function results($param){
$data = [];
//some other code
$this->load->view("election/header");
$this->load->view("election/menu");
$this->load->view("election/results", $data);
$this->load->view("election/footer");
}
This is perfectly working, but when I had a second parameter, miraculously, the method didn't display anymore the header/menu/footer. Only the custom view result...
I also found that if I randomly add another parameter to the previews method results for exemple, the same kind of bug happens. The header/footer/menu aren't normally displayed anymore.
I guess there is a problem with the routing/htaccess or segment... but I can't put my finger on it...
Here is the root .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^kanri(/(.*))?$ admin.php?/$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user(/(.*))?$ user.php?/$2 [L]
</IfModule>
And the admin/routing.php
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Thanks for your time!
Thanks to D. Dimitrov, the solution was simple. I just had to replace the relative path by using the site_url() function. I haven't totally understand the why but since it's working, it's all gud :>

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!

how to set homepage in Codeigniter?

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.

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

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