Symfony2: How to get request parameter - symfony

I have a route:
professor_identity_edit:
path: /professor/{id}/identity
defaults: { _controller: FrontBundle:ProfessorIdentity:edit }
How can I get the parameter {id} in twig.html file ?
I already tried app.request.query.get('id') but with no success.

Not a good thing but you can still do. Normally you would pass the info to Twig from the controller like #Maerlyn pointed out.
Get route parameters in Twig.
{{ app.request.attributes.get('_route_params') }}
AND
Gets whole bundle name in Twig.
{{ app.request.attributes.get("_controller") }}
Get route name in Twig.
{{ app.request.attributes.get('_route') }}

I found the solution:
{{ app.request.attributes.get('_route_params')['id'] }}

Related

get route controllers/action within rendered subcomponent in symfony3

In my base.html.twig I render a component:
{% block header %}
{{ render(controller("AppBundle:Application\\Header:header")) }}
{% endblock %}
Is there a way to get the current route action/controller? i.e. the current url in the browser?
When I do var_dump($request->get('_route'));die; it results in null
If you want get the actual route, in your controller you can get the master request like this:
$this->container->get('request_stack')->getMasterRequest()->get('_route');
As I couldn't answer through a comment, I have to write this as an answer.
As mentionned above, this question might be a duplicate of:
Get current URL in Twig template
To get both the name of the current route and the current URL you can simply render them within your twig template by:
<p><strong>current route name</strong> :{{app.request.attributes.get('_route')}}</p>
<p><strong>current url: </strong> {{ app.request.schemeAndHttpHost ~ app.request.requestUri }}</p>

symfony2 namespaces using render(controller(..))

When I try to call:
{{ render(controller(
'AppBundle:Default:_test'
)) }}
this one works, but if I create Admin folder and move my DefaultController there and call it like that:
{{ render(controller(
'AppBundle:Admin:Default:_test'
)) }}
this error shows up
("Unable to parse the controller name
"AppBundle:Admin:Default:_test".")
What I'm missing? Thanks.
If you moved the controller from Default to Admin/Default folder, you should call it with
{{ render(controller(
'AppBundle:Admin/Default:_test'
)) }}

Symfony Twig path() not working when using render

I have a layout that includes some chuck of code form a controller called "Layout"
In the header section I have:
{% block accessinfo %} {% render "/layout/accessinfo" %} {% endblock %}
It works pretty fine, the view file content is:
{% extends '::layout.html.twig' %}
{% block body %}
{% if( is_logged == 0 ) %}
Welcome, access your <a id="accessAccount" title="Access your account">here</a>.
{% else %}
Hi, <b><em> {{ is_logged_user_name }}</em></b>, <a id="doLogout" href="javascript:void;">(Logout)</a>.
<i class="icon-user"></i> Your Account
{% endif %}
{% endblock %}
As one can figure out, path('account/manage') points to the Route named 'account/manage', but it's not returning the fully qualified URL to my project.
It returns:
http://localhost.project/account/manage
where it should be:
http://localhost.project/web/app_dev.php/account/manage
NOTE: I have path() all around my template files and they work like a charm.
IMPORTANT: I found out that when I call REQUEST URI inside the action method:
$this->get('request')->server->get('REQUEST_URI')
PHP will return the URL called by the render, in this case is:
/layout/accessinfo
Perhaps I'm not fully understanding your issue but it seems like you missunderstood the use of the path() and render() functions.
First of all if you like to render a controller and you follow the documentation here you would do it like this...
{{ render(controller('AcmeArticleBundle:Article:recentArticles') }}
{# with some parameters #}
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
This assumes you're using Symfony >= 2.2. This follows the bundle:controller:action pattern, which is called Controller Naming Pattern
For a normal use of the path() function you would always use the name of the route and not a hardcoded URL (as it seems like you're passing in URLs and not route names?)
Let's say your route is called accountmanager, your routing.yml should look like this example
# app/config/routing.yml
accountmanager:
path: /account/manage
defaults: { _controller:YourBundleName:YourControllerName:ControllerAction }
And with that in your routing.yml in twig the use of path() is simply achieved by writing {{ path('accountmanager') }}
See the documentation on this topic. Using the name of the route and not a URL pattern ensures that you're getting to the right page which also includes your environment settings (like app_dev.php for your dev environment)

Get current route/path deliver _internal route

I'm rendering data from a CMS via Twigcode to a Controller and the Controller renders a twig template. I'd like to have the current route. I already tried to use:
// Controller
$request = $this->container->get('request');
$routeName = $request->get('_route');
// or Twigtemplate
{{ app.request.attributes.get('_route') }}
But the result is "_internal". How can I solve that problem?
You forgot the call to the Twig path function in your Twig template!
{{ path(app.request.attributes.get('_route')) }}
instead of
{{ app.request.attributes.get('_route') }}
Hope it helps!

twig: find out if I'm in a subroute of a mainroute

I'm trying to exclude a menu if I'm in a subroute of a mainroute.
So for example this:
if route is /backend/* then exclude
else if route is /backend/admin/* don't exlude
else if route is /backend/admin/create then exclude
Is there a way to achieve this in twig with a few lines of code?
To get current path in twig you can use app.request.pathinfo.
Edit:
If you want to get route name of current url you can do it in following way.
Add Router object as template global variable.
#In app/config.yml
#.......
# Twig Configuration
twig:
globals:
router: #router
And then in your template,
{% set route_info = router.match(app.request.pathinfo) %}
{% set route_name = route_info._router %}
Then you can include/exclude your template based on route name.
Edit Again
Route name can be found in much simpler way.
{{ app.request.attributes.get('_route') }}

Resources