I have a site called: orders.
In the controllers folder I have a sub folder called: manage.
In there I have a controller called: editOrder
In the editOrder controller I have an index function that get an $id as a parameter.
In one of my forms I have a link to: editOrder/1
In my route file I have this code: $route['editOrder'] = 'manage/editOrder';
The link gives the error page not found.
I tried to go to the page manually, like this: http://localhost/orders/editOrder/1
Page not found
I tried this way:
In my route file:
$route['editOrder/(:num)'] = "manage/editOrder/$1";
Page not found
I have changed my config file to:
$config['uri_protocol'] = 'PATH_INFO';
$config['enable_query_strings'] = TRUE;
And tried this way:
http://localhost/orders/?c=editOrder&m=index&id=1
That takes me to the home page.
How can I pass the id segment to the editOrder controller?????
Ahhhhhhhhhh
How would I call this controller????
Make sure:
Your custom route comes after the 2 default ones. So it should be:
$route['default_controller'] = "defaultController";
$route['404_override'] = '';
$route['editOrder/(:num)'] = "manage/editOrder/index/$1";
Your controller file editOrder.php has a class editOrder extends CI_Controller and is inside the folder "controllers/manage/";
Your editOrder controller has a function index($id) {} method;
To sum up, if you're going to call a method, you need to specify it. In case of routing, that means you have to specify even the index() method.
Related
I'm having some troubles with routing in codeigniter.
My routing file is as below:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
$route['admin/create'] = 'posts/create';
$route['admin/listposts'] = 'posts/list';
$route['admin'] = 'admin/index';
$route['posts/(:any)'] = 'posts/view/$1';
$route['posts'] = 'posts/index';
$route['default_controller'] = 'pages/index';
$route['(:any)'] = 'pages/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
All routes work just fine except for the first two:
$route['admin/newgallery'] = 'gallery/do_upload';
$route['admin/listgallery'] = 'gallery/list';
When I type mypage/admin/listgallery it calls gallery/list correctly. The problem is when I type the address with the original controller/method (in this case gallery/list) it goes to the list page as well when it should call a 404 error! Every other routing rule I have set does that, except the first two!
Out of the Box, Codeigniter allows you to directly access any Controller/Method from the URL.
It also provides the creation of custom routes so you could have 10 or more urls all pointing at the same controller/method with parameter passing if that was your desire...
So in the case you ONLY want access to any Controller/Method that are defiend in the Routes config.
You need to test if the url is defined in the routes config array.
The main code is something like...
$this->load->helper('url');
if(!isset($this->router->routes[uri_string()])){
show_404(); // Or whatever you want ...
}
And you would put this in your controller's constructor you want to protect.
Of course you could create a common controller and extend those controllers you want to protect in this manner.
( NOT Recommended ) Or if you want to get really "hacky" you could put it in the system/core/controller constructor and make it system wide. SO Everything needs to be defined in a route.
NOTE: This breaks the 'default_controller'.
I have to call a function from one controller to another controller.
I need to call a function from testAction() in testController to indexAction() in anotherController.
How could i call that one. I tried below ones. but that doesn't seems to work. Please advice me.
$this->_redirect("./admin/another/index", array("group_id" => $group_id));
$this->_forward($action = 'indexAction', $controller = 'adminhtml_another', $module = 'custommodule');
If you are redirecting from one controller to another
$this->_redirect('customer/account/login');
//Format: $this->_redirect('module/controller/action');
If in the same controller
//$this->_redirect('*/*/SameControllerMethodAction');
UPDATED:
Your solution :
$this->_redirect('adminhtml/newsletter_template/index');
with
Adminhtml - module
Newsletter (Folder) > Template - controller
Index - function
I found the answer. now redirection working fine. i have just add below 'return;' statement. the code is,
$this->_redirect("adminhtml/newsletter_template/");
return;
My Current URL Is
http://myaliveidea.com/news/detail/hello/71/In-Ireland-hiking-for-ancient-relics-hidden-by-fog
Then i want to remove controller name
In this URL the controller name is == detail
and function name id = hello
So i want my URL Like this
http://myaliveidea.com/news/hello/71/In-Ireland-hiking-for-ancient-relics-hidden-by-fog
Try this code in routes.php :
$route['hello/(:any)'] = "detail/hello/$1/$2";
or
$route['hello/(:num)/(:any)'] = "detail/hello/$1/$2";
in application/config/routes.php, rewrite the url like this:
$route['news/hello/(:num)/(:any)'] = "news/detail/hello/$1/$2";
I am not sure about the news part, if it is a subfolder in the controller folder than the above line is ok, if CI in installed in news subfolder, then please remove the news part from both side.
Make sure to accept the parameter values from the hello method like this:
public function hello($id = null, $slug = null)
You can change uri path with routes, here there is documentation
exmple: this load default controller/class with function page,
www.example.com/page
unless we have controller/class named page AND set $route['page'] = 'page'; it'll load the controller. But if we dont set the $route, it'll still load default_controller.
is that true a controller must have a $route[''] always? is not it possible to load controller page without set $route[''] even there is no default controller function with same name?
Edit:
I access
www.mysite.com/index.php/user
I do have user controller with index function, but my route file only contain:
$route['default_controller'] = 'page';
$route['(:any)'] = 'page/$1';
$route['product'] = 'product';
//$route['user'] = 'user';
$route['404_override'] = '';
returns 404, only works if I uncomment this: $route['user'] = 'user';
why?
Thanks.
No, that's not true. CodeIgniter, by default, directly maps URI segments to:
example.com/index.php/controller/method/param/param/...
Or if you have an .htaccess / similar solution to remove index.php:
example.com/controller/method/param/param/...
Routing is used when you wish to use a URL that does not directly map to this convention.
Edit: You have conflicting routes. CodeIgniter will look at each route in order from top to bottom, and if it find one that matches, it stops looking and processes that route. Because you have an (:any) catch-all route, it will match anything (like it says).
The rule of thumb is to place your most specific routes first, and then get more generic and catch-all later. Your (:any) route should be the very last one in your list. And the default controller and 404 overrides should stay first.
$route['default_controller'] = 'page';
$route['404_override'] = '';
$route['product'] = 'product';
$route['user'] = 'user';
$route['(:any)'] = 'page/$1';
You need to add the product and user routes because you've defined the (:any) route. If you want to avoid writing route rules for every one of your existing controllers, but still take advantage of a catch-all controller, consider using the 404_override controller/method instead. You can do your verifications to check if the URI is valid there. Just make sure to throw a 404 error if not (you can use show_404()), since any non-existant URL will be routed to there.
I recently upgraded a site from CodeIgniter 1.7.x to 2.0.3. About the same time someone in my organization requested we add some pages to the site. Underneath a section. In the old version of the site I used some workarounds in the controller to break up a longer URL. But in version 2 I see that I should be able to use subdirectories in the controllers folder to do it in a more proper way. After looking all over the place I've tried all sorts of routing declarations and fiddled with all sorts of things. Hopefully I'm doing something simple, wrong, or perhaps someone has seen a similar issue stemming from the upgrade.
I'm trying to get the URL from something like:
/about/locations
Which used to work with a controller named about.php. To something more like:
/about/social_responsibility/commitment
Where about is now a aub-directory.
Funny thing is, currently it does sorta work. That second URL displays correctly. However my old pages, that first URL, now do not function... My new structure uses a base.php (default_controller) in the about directory. Thus if I write:
/about/base/locations
It does work. But I thought the whole routing thing (default controller) and using subdirectories is supposed to clean the URL up.
My info is as follows...
Current Routing (it's changed a bunch over the last few hours)
$route['default_controller'] = "base";
$route['404_override'] = '';
$route['about'] = "about/base";
Directories and Files
/controllers/about/base.php
/controllers/about/social_responsibility.php
Chunk of base.php
class Base extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->data['parent'] = "About";
$this->load->model('mnav');
}
public function index()
{
}
public function locations()
{
}
}
I also have a MY_Controller that extends CI_Controller, but all it does is enable FirePHP for me in the development environment.
Anyone have any clues? Or need some more info to help? Thanks!
I just tested on my system, with CI 2.0.2 and it seems that the default controller setting works for subdirectories as well, without any other routes.
// so in your config file, whatever your default_controller is set to...
// you would just use that as the name of the `base` controller in about
// for example, if your default_controller is 'welcome'
// in /application/config.php
$route['default_controller'] = "welcome";
$route['404_override'] = '';
// then, it should work for the subdirectory where there is a controller
// named 'welcome'
// application/controllers/about/base.php
class Welcome extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo "I can be reached with /about";
}
}
So, all you should have to do is remove all of the routes for about
// remove this
$route['about'] = "about/base";
Important
This will only work when accessing /about -- anything in additional segments will be looking for additional controllers. So you'd have to think about how you'd like to access the base controller (whether or not you name it something else).
I'd assume that /about/locations is looking for an actual "locations" controller in your about folder, rather than being the method for your base controller. As far as CI knows, you're trying to execute one of two functions:
About controller's "locations" method
about/Locations controller's index method (obviously doesn't exist)
It follows that any 3-segment or greater URI will work with this scheme, but 2-segment URI's will confuse it. I don't think letting CI fallback to the default controller is going to work here. Try this:
$route['default_controller'] = "base";
$route['404_override'] = '';
$route['([^\/]*)'] = '$1/base';
$route['([^\/]*)/([^\/]*)'] = '$1/base/$2';