Determine Action for Route in Symfony 1.4 - symfony1

I'm writing an extension of url_for which will check that the logged in user has the appropriate credentials to access a link to a route before rendering said link.
I have determined how to get the Action from module/action strings, and successfully check the credentials required. But I can't find a reliable way to get the appropriate Action from the name of the route, whether a named route or one derived from pattern matching e.g.
home/index => module: home, action: index
#home => module: home, action: index
I suspect I need to copy code from sfRouting, as I can't find a public method in Symfony to do this. Is there a better way?

I tend to just use the route name for stuff like this:
<?php $route = $sf_context->getInstance()->getRouting()->getCurrentRouteName() ?>
<?php if ($route == 'home'): ?>
<p>hello</p>
<?php endif ?>

Related

ZF2 ACL check link in view

I have set up my roles, resources and permissions in my bootstrap, and in my layout have set up a navigation menu based on this, and this works.
What I am attempting to do now is create an admin panel with edit / delete links IF the current logged in user has those permissions. e.g. I may have multiple roles that can view a list of cms pages, but only certain roles can edit a cms page, and only certain roles can delete a cms page.
At the moment I am just checking if the user is logged in:
<?php if($user = $this->identity()): ?>
<?php if($user['role'] == 'admin'):?>
Delete
<?php endif;?>
<?php endif;?>
How do I check the permissions of the current user role for the specified resource from the view for an arbitrary link (as above)?
The ACL view helper is injected into the layout, so to check if a role has access to a resource, we can call $this->layout()->acl->isAllowed.
In this code snippet, we check if the user is logged in ($this->identity() returns false if not logged in, or an array of details if logged in), then if the user has 'delete' permission to the resource:
<?php if($user = $this->identity()); //is logged in? ?>
<?php if($this->layout()->acl->isAllowed($user['role'], $resource, 'delete')):?>
Delete
<?php endif;?>
<?php endif;?>
isAllowed signature is isAllowed($role = null, $resource = null, $privilege = null)

Routing in Kohana without showing controller name

I want to create a routing in Kohana Framework Version 3.3.1.
I want URL like http://www.test.com/male/London and internally they will act like below URL-
http://www.test.com/list/search/London
I want to hide the controller and action name from the URL.
Any help is greatly appreciated.
This can be accomplished by using routes in bootstrap.php and/or a module's init.php file.
For example, you could set up a route for (male/<location>) and then your default controller would be list and the action search.
You could then access the location within the controller/action by using $this->request->param('location'); to be used in whatever DB query you need it.
Confused? Have a read through this section of Kohana Docs and it should all make sense.
You have to do two changes in your file:
Bootstarp file :
Route::set('list', 'male/<id>' )
->defaults(array(
'controller' => 'list',
'action' => 'search',
));`
And second is you can make your link like
href="<?php echo URL::site('male/'.id, TRUE) ?>">
And this route file should above at your default route file.

How to check for invalid route in layout view

I have this piece of code written in Application/view/layout/layout.phtml for adding a link which will be pointing to current page. This I added for debugging purpose for refreshing the current page.
<li class="active"><?php echo $this->translate('Refresh') ?></li>
This is working fine as long as I don't invalid route (404 error). I get below fatal error
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException'
with message 'No RouteMatch instance provided'
I wan't to handle this by adding a condition to check if its a 404 error or an invalid route before trying to render the url. I am not sure how to do it. I tried to look the source code of Zend\View\View class and ViewModel class to see if there is a way I can get error code or something which can tell its a 404 error.
Edit:
As a last resort I have adding the try catch block like below which is working fine but want to check if there is any elegant way
<?php
try{
$url = $this->url();
?>
<li class="active"><?php echo $this->translate('Refresh ') ?></li>
<?php
} catch (Exception $ex) {
}
?>
The exception message you are getting is not because the route is undefined but rather that the routeMatch parameter of the URL view helper, Zend\View\Helper\Url, has not been set.
Normally when using the URL helper you can provide the name of the route as the first argument. The helper will internally use the router and route match to generate the correct URL string based off the route configuration.
For example
echo $this->url('test'); // will create the route matching the test route name
The helper also allows you to provide no route name, $this->url(), in which case it will use the last matched route by default when creating theses routes. There is however one special case when you cannot rely on this; when the application cannot match a route, there will be no RouteMatch available.
So using this shorthand convenience method in the layout.phtml when the application tries to load the 404 error template (which is wrapped by the layout.phtml) the URL view helper will have no RouteMatch available and throw the error you received.
To fix this you should always provide a route name when calling the URL view helper in the layout or error scripts.

Route to default not found 'catch all' address

I'm trying to route URLs like stats/non-existent-page to a default 'not found' page, where I'd like to display a 'Did you mean' suggestion along with a not found message, how can I route all non-existent URLs to a default controller/action?
Something like:
match 'stats/*path' => 'default#non_existent'
Add this after your other 'stats/..' urls , This will route your all stats/* paths to the controller action mentioned with path variable you can use to determine your 'did you mean' suggestion..

Symfony/Routing: Using POST to Avoid Query Params in URL

I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
Using Symfony, I have created a sign-up done page whose URL should be /signup/done
When the form is processed, I want to redirect to the signupSuccess action and pass the recently created ID for future use. So I use...
$this->redirect('#signup_done?id=' . $sign_up->getId());
The routing is as follows:
signup_done:
url: /signup/done
param: { module: SignUp, action: signupDone }
I have avoided the :id at the end because I don't want it in the URL.
But the resulting URL is /signup/done?id=1
Just as an experiment, I tried putting this on a template.
<?php echo link_to('Sign-up again', '#signup_done?id=1', 'post=true') ?>
Even when using post, the query parameter appears in the URL.
The need is: I want to pass the id from one action to the next action, but I do not want it seen in the URL. Is there a way to hide it?
I set the id as a parameter in the request using $request->setParameter('id', $id) and it was available in the next action.
This kept the URL clean.
If you want to post, you need a form. Back in symfony 1.2 there were helpers that you could call and made you just that - but they were removed for the promotion of promoting good code.
Depending on how you want the 'Sign up again' text to look, you can either create a simple form and a submit button, or create a link, attach a click listener, and create a form there via JS, finally post it.
Any parameter that you pass to the route in url_for, link_to and such end up in the get parameters.

Resources