Is there way of routing controllers in CodeIgniter such that a controller with a url like path/controllerA/some_method becomes /some_name/some_method_ofA where some_name is controller A without moving controller A?
I can do this properly with one method of controller a via:
$route['some_name/(:any)'] = '/path/controllerA/some_method/$1';
but I can't seem to get it so that some_name is basically an alias for controller A such that I can access other methods.
So before: path/controllerA/some_method_ofA, path/controllerA/some_method_ofA2
After: /some_name/some_method_ofA, /some_name/some_method_ofA
Yes, you can set it up:
$route['signin'] = 'Contact/SignIn'; //Maybe that, controller contact and method SignIn
$route['page/(:any)'] = 'Post/index/$1';
You just need to use:
$route['new_url'] = 'Controller/method';
Related
I have a route and I need to know a controller that would be used for it.
I know how to find a controller for the current route:
Illuminate\Support\Facades\Route::currentRouteAction();
But how can I do the same for other routes?
Route Facade is the answer. It can return Illuminate\Routing\RouteCollection object.
and then you can get Illuminate\Routing\Route object by route name.
Every Route triggers multiple actions such as middleware and controller methods. So we need only the controller.
use Illuminate\Support\Facades\Route as RouteFacade;
/*#var $route Illuminate\Routing\Route*/
$name = 'admin.reports.my-report.get-filters'; // sample route name
$route = RouteFacade::getRoutes()->getByName($name);
$controllerAction = $route->action['controller'];
$controller = explode('#', $controllerAction)[0];
logger($controller);
P.S.
In cases like that - remember to make a unit test for this functionality to be sure it works as you upgrade your laravel.
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 want to load a controller function from another controller function using codeigniter. What is the suitable way to do this so when call it url should be changed also.
No You cant do it.
What you have to do it is create that function in model and call it through your controllers. So it will work fine.
Ex
In Model
function get_id()
{
//some argument
}
In controller 1
$this->Model_name->get_id()
In controller 2
$this->Model_name->get_id()
yes you can (for version 2)
load like this below inside your controller
$this->load->library('../controllers/whathever');
and call the following method:
$this->whathever->functioname();
I am attempting to create a route in Laravel for a dynamic URL to load a particular controller action. I am able to get it to route to a controller using the following code:
Route::get('/something.html', array('uses' => 'MyController#getView'));
What I am having trouble figuring out is how I can pass a variable from this route to the controller. In this case I would like to pass along an id value to the controller action.
Is this possible in Laravel? Is there another way to do this?
You are not giving us enough information, so you need to ask yourself two basic questions: where this information coming from? Can you have access to this information inside your controller without passing it via the routes.php file?
If you are about to produce this information somehow in your ´routes.php´ file:
$information = WhateverService::getInformation();
You cannot pass it here to your controller, because your controller is not really being fired in this file, this is just a list of available routes, wich may or may not be hit at some point. When a route is hit, Laravel will fire the route via another internal service.
But you probably will be able to use the very same line of code in your controller:
class MyController extends BaseController {
function getView()
{
$information = WhateverService::getInformation();
return View::make('myview')->with(compact('information'));
}
}
In MVC, Controllers are meant to receive HTTP requests and produce information via Models (or services or repositores) to pass to your Views, which can produce new web pages.
If this information is something you have in your page and you want to sneak it to your something.html route, use a POST method instead of GET:
Route::post('/something.html', array('uses' => 'MyController#getView'));
And inside your controller receive that information via:
class MyController extends BaseController {
function getView()
{
$information = Input::get('information');
return View::make('myview')->with(compact('information'));
}
}
I want to pass parameters to the main action of a controller like so :
http://www.site.com/controller/index/param1/param2
but I DON'T want to have to write the 'index' action. So :
http://www.site.com/controller/param1/param2
How can I do this in codeigniter ? Thanks :)
You will have to modify your routes.php as the route default would be to controller/method
And explicitly define your route of
$route['blogs/(:any)/(:any)'] = "blogs/index/$1/$2";
Word to the wise about routing, once you set down this path of 'prettying up' the controller/method, you will need to define it for all methods in your controller:
$route['blogs/load/(:any)'] = "blogs/load/$1";
$route['blogs/delete/(:any)'] = "blogs/delete/$1";
As you won't be able to just call /blogs/load/4332 it will think 'load' is var 1 and '4332' is var 2.
Reference: http://codeigniter.com/user_guide/general/routing.html