Uri rerouting codeigniter - php

I am struggle with URI rerouting in my codeigniter version(2.2.0)application.
Here is my routes.php
$route['details/(:num)'] = 'agent/manage_agents/$1';
And in my view file
foreach($data as $value) {?>
Manage
}?>
But i will get 404 error.
In my Controller file
class Agent extends CI_Controller {
public function manage_agents($id)
{
echo $id;
}
}
UPDATE
Finally i found Which causes the problem.In my routes.php
$route['(:any)'] = "spotmyticket/$1"; when hide this line everything works fine.
Here is my complete routes.php
$route['404_override'] = '';
$route['default_controller'] = "spotmyticket";
$route['ticket']="ticket";
$route['ticket/(:any)'] = $route['ticket'].'/$1';
$route['captcha'] = "captcha";
$route['captcha/(:any)'] = "captcha/$1";
$route['admin'] = "admin";
$route['admin/(:any)'] = "admin/$1";
$route['userdashboard'] = "userdashboard";
$route['userdashboard/(:any)'] = "userdashboard/$1";
$route['fbci'] = "fbci";
$route['fbci/(:any)'] = "fbci/$1";
$route['(:any)'] = "spotmyticket/$1";
$route['agent-management'] = 'agent/index';
$route['register'] = "agent/agent_register";
$route['test'] = 'agent/test';
$route['details/(:any)'] = 'agent/manage_agents/$1';

Please define $route['(:any)'] = "spotmyticket/$1"; as the last rule in routes.php. Everything will be fine. This rule is for any url so all rules defined after this rule will be ignored.

If you are writing this route:
$route['details/(:num)'] = 'agent/manage_agents/$1';
so that you may pass any integer value (like you are passing $value['id']), then try using (:any) instead of (:num). So your route should be:
$route['details/(:any)'] = 'agent/manage_agents/$1';

Related

CodeIgniter $route don't work in CI 3

The CodeIgniter Routing dont work,
Im trying to route:
$route['Kayit'] = "Kayit/index";
localhost:8090/Kayit/ result = 404 Page Not Found
And it doesnt work. Can someone help me?
My route.php file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['default_controller'] = 'Sayfa/view';
$route['(:any)'] = 'Sayfa/view/$1';
$route['Sayfa/'] = 'Sayfa/view/';
$route['Kayit/'] = "Kayit/index";
?>
Put
$route['(:any)'] = 'Sayfa/view/$1';
At the end line since wildcard placeholder intercepts other routes.
Like:
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['default_controller'] = 'Sayfa/view';
$route['Sayfa'] = 'Sayfa/view';
$route['Kayit'] = "Kayit/index";
$route['(:any)'] = 'Sayfa/view/$1';// (:any) wildcard catcher must go at the end
Also, you are good to remove trailing slashes from routes.
From docs:
Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

How to route multiple controllers in codeigniter?

I got error "Unable to determine what should be displayed. A default route has not been specified in the routing file"
I have two controllers 1.signup and 2.login_control !
How to set routes in routes.php file ? my baseurl is http://localhost:1337/PhpProject1/
my code is below in routes.php file
$route['signup/(:any)'] = 'signup';
$route['login_control/(:any)'] = 'login_control';
$route['404_override'] = '';
First, specified your default route by your first controller.
Like this-
$route['default_controller'] = "signup";
and then add your another controller.
$route['login-control'] = 'login_control';
Do like this
Syntax
$route['how_its_look_like'] = 'controller_name/method';
Example
$route['signup'] = 'login/signup';
$route['login'] = 'login/login';
$route['logout'] = 'login/logout';
$route['404_override'] = '';
Links
Ccodeigniter Routing Examples

codeigniter routing uri with default_controller

Hi everyone im new with codeigniter. my routes.php is
$route['default_controller'] = "Maincontroller";
$route['(:any)'] = "Maincontroller/user_index/$1";
i want to search people if they type in the URL = www.site.com/username
but my problem is when going to other controller. should i route all my controllers?
$route['default_controller'] = "Maincontroller";
$route['somecontrollers'] = 'somecontrollers';
$route['(:any)'] = "Maincontroller/user_index/$1";
then how about my methods.
i tried this remap
public function _remap($method, $params = array())
{
if (method_exists(__CLASS__, $method)) {
$this->$method($params);
} else {
$this->user_index($method);
}
}
but this only works properly in controller which is not default, and i get the result i want. but as i apply this in my default controller it doesnt work well.
Hope this one help you!
$route['Maincontroller/(:any)'] = 'Maincontroller/user_index/$1';
$route['somecontrollers/(:any)'] = 'somecontrollers/user_index/$1';
$route['(:any)'] = "user_index/$1";

Hiding or removing controller name in url using routes for seo purpose = codeigniter

I am using one controller. How can I remove or hide the controller name on my URL using routes?
I already set my .htaccess to remove my index.php. I've tried lots of code from other similar questions but I don't have luck. I don't know If I have a problem on my live server because on my previous project I used this code
$route['del/(:any)'] = "my_cotroller";
$route['(:any)'] = 'pages/view/$1';
and this works perfectly fine but when I tried to use it on my current project it didn't work.
This is my default route
$route['default_controller'] = "my_cotroller";
$route['404_override'] = '';
I want to archive a URL like http://www.sample.com/login but my current route code gives me this http://www.sample.com/my_controller/login.
Thank's for help.
Simple approach when i use CI:
Example Controller:
<?php
class Haha extends CI_Controller
{
public function check()
{
$this->load->view('path/to/check/view');
}
public function choke()
{
$this->load->view('path/to/choke/view');
}
}
Example routes:
//same name with C method
$route['check'] = 'haha/check';
$route['choke/(:any)'] = 'haha/choke';
//or custom route name
$route['squirrel'] = 'haha/check';
$route['woohoo/(:any)'] = 'haha/choke';
My htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|uploads|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
Access the uri:
http://yoursite.com/check
http://yoursite.com/choke/whatever
//or
http://yoursite.com/squirrel
http://yoursite.com/woohoo/whatever
Addition:
Here is complete routes from my old project using CI, maybe you can get something.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'sitehome';
$route['page'] = 'sitehome/page';
$route['page/who-we-are'] = 'site/whoweare';
$route['page/why-use-us'] = 'site/whyus';
$route['page/about-us'] = 'site/about';
$route['page/contact-us'] = 'site/contact';
$route['page/news'] = 'site/news/news';
$route['page/topics'] = 'site/topic';
$route['page/polls'] = 'site/polls';
$route['page/results'] = 'site/result';
$route['agent/detail/(:any)'] = 'site/agentdetail';
$route['register'] = 'site/register';
$route['login'] = 'site/login';
$route['user/home'] = 'usermain';
$route['user/edit-account'] = 'user/editaccount';
$route['user/retire'] = 'user/retired';
$route['user/business/data'] = 'user/business';
$route['user/business/topic/create'] = 'user/topic/topiccreate';
$route['user/business/topic/view'] = 'user/topic/topicview';
$route['user/business/topic/edit/(:num)'] = 'user/topic/topicedit';
$route['user/inbox'] = 'user/inbox';
$route['user/message/read/(:any)'] = 'user/readmessage';
$route['user/provider/list'] = 'user/providerlist';
$route['user/provider/send-message/(:any)'] = 'user/providerlist/sendmessage';
$route['user/notification'] = 'user/notified';
$route['user/inquiry'] = 'user/inquiry';
//testing purpose
$route['percobaan'] = 'coba/percobaan';
$route['poll'] = 'site/polls/retrievePoll';
//serve response
$route['response/single-poll'] = 'api/ResponseData/serveHomePoll';
$route['response/multi-poll'] = 'api/ResponseData/servePoll';
$route['404_override'] = 'notFound';
$route['translate_uri_dashes'] = FALSE;
/* End of file routes.php */
/* Location: ./application/config/routes.php */
Notes :
http://example.com/business/ will fail or not exist.
Good luck.
You can define a custom route in config/routes.php - for example:
$route['about'] = 'name_controller/about';
Then, http://example.com/about
goes to http://example.com/name_controller/about
See more http://ellislab.com/codeigniter/user-guide/general/routing.html

How to hide controller name in the url in CodeIgniter?

so the thing is I'm using .htaccess to hide the index.php but I still get the controller name in the url like that: http://example.com/name_controller/about
My question is: is it possible to hide the name of the controller, so that only method is shown? hxxp://example.com/name_controller/about
You can define a custom route in config/routes.php - for example:
$route['about'] = 'name_controller/about';
Then, http://example.com/about
goes to http://example.com/name_controller/about
See Hiding the controller’s method name in the URL? in the CI forums for more information.
You can add an entry in the /system/application/config/routes.php file:
$route['about'] = "controller_name/about";
$route['default_controller'] = "xxx";
home
$route['home'] = "xxx/home";
function_name/parameter0/parameter1/parameter2
$route['Collection/(:any)'] = "xxx/Collection/$1";
I did it like this: (config/routes.php)
Code:
$route['((photos|blogs).+)'] = "$1";
$route['([a-zA-Z0-9_-]+)'] = "user/profile/$1";
it is ok correct solutions for common.
You can add below code in the /application/config/routes.php file:
$route['default_controller'] = "Home";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(?i)about'] = "Home/about";
$route['(?i)login'] = "Home/Login";
$route['(?i)products'] = "ProductController/ProductList";
$route['(?i)addproduct'] = "ProductController/AddProduct";
$route['(?i)editproduct'] = "ProductController/EditProduct";
$route['(?i)products/(:any)'] = "ProductController/ProductList/$1";//search product list perameters.
100% It work..

Categories