Feature Request #3497
Allow a controller to query the name of the matched route
| Status: | Closed | Start date: | 12/21/2010 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | Core | |||
| Target version: | - | |||
| Resolution: | wontfix | Points: |
Description
Currently a controller can get all the parameters from a matched route, as described in the documentation ( http://kohanaframework.org/guide/tutorials.urls ), but when you have several routes with different parameters - and when some controllers can be accessed for more then one route - it useful to have different logic depending on the route that was taken to get to the controller.
For example one route requires a parameter to be provided in the URL while the same controller can be hit with another route that does not require that parameter and in which case the controller should ask the user to enter that parameter manually. In such a case the controller could check if the route that includes the parameter was triggered and only then try to access the parameter in the route params.
This behavior can currently be implemented by adding a "default param" with the name of the route for each of the route, like:
Route::set('accounts', '<account>/<controller>(/<action>(/<id>))')
->defaults(array(
'name' => 'accounts',
'controller' => 'home',
'action' => 'index',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'name' => 'default',
'controller' => 'home',
'action' => 'index',
));
but that is a bit ugly - it would be much more useful to allow the controller to query the request for the selected route's name.
History
Updated by Jeremy Bush over 1 year ago
- Target version set to v3.2.0
So do you have some kind of "fix" for this? It's a little ambiguous what should be done here.
Updated by Oded Arbel over 1 year ago
Well, Request::route is a public instance field that contains the route that was matched. Getting access to its name using $request->route->name would seem to me pretty much straight forward, except for the fact that the Route object doesn't know its own name.
A simple fix might be:
Index: system/classes/kohana/route.php
===================================================================
--- system/classes/kohana/route.php (revision 40208)
+++ system/classes/kohana/route.php (working copy)
@@ -68,7 +68,7 @@
*/
public static function set($name, $uri, array $regex = NULL)
{
- return Route::$_routes[$name] = new Route($uri, $regex);
+ return Route::$_routes[$name] = new Route($name, $uri, $regex);
}
/**
@@ -173,6 +173,9 @@
// Create a URI with the route and convert it to a URL
return URL::site(Route::get($name)->uri($params), $protocol);
}
+
+ // Route's name from the route setup
+ public $name = '';
// Route URI string
protected $_uri = '';
@@ -198,7 +201,7 @@
* @return void
* @uses Route::_compile
*/
- public function __construct($uri = NULL, array $regex = NULL)
+ public function __construct($name, $uri = NULL, array $regex = NULL)
{
if ($uri === NULL)
{
@@ -210,6 +213,8 @@
{
$this->_regex = $regex;
}
+
+ $this->name = $name;
// Store the URI that this route will match
$this->_uri = $uri;
What do you think?
Updated by Andrew Coulton over 1 year ago
Or just use Route::name($this->request->route)
Though I can't particularly see a reason not to store the name in the object as you suggest.
Updated by Oded Arbel over 1 year ago
Indeed so, though it is a bit ungainly. I missed that API until you mentioned it, and I think that having the name accessible directly from the route object is more straight forward and easier to understand.
I'd appreciate it if we can get this change into a future Kohana release.
Updated by Jeremy Bush 12 months ago
- Target version changed from v3.2.0 to v3.3.0
I'm not entirely convinced about this. Routes can be "anonymous" as well, and Route::name() seems to work well.
Updated by Oded Arbel 12 months ago
If routes can be anonymous, in which case - what will Route::name() return? I assume the numerical index of the route in the routes list? I don't see how that is better then "null" and in which case there is no problem to return "null" from Request->route->name.
Anyway, Route::name() indeed works well, it just less discoverable and friendly then having a simple attribute on the route object - nor do I think the use cases conflict BTW, having one does not mean removing the other.
I think this small change should not warrant such lengthy discussion, so if you don't believe this to be a worthwhile pursuit then I suggest closing this bug as won't fix. I will not promise not to complain, though ;-)
Updated by Jeremy Bush 5 months ago
- Target version changed from v3.3.0 to Unscheduled
Updated by Woody Gilk 4 months ago
- Category set to Core
- Status changed from New to Closed
- Assignee set to Woody Gilk
- Target version deleted (
Unscheduled) - Resolution set to wontfix
We already have Route::name() and as Jeremy said, we have to support anonymous routes.