symfony2 enable to find routing error for optional parameter - symfony

I would like to add a format to my route, but this error is appearing always:
No route found for "GET /ecomerce/0" (from "http://localhost/ecomerce/web/app_dev.php/ecomerce/")
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException
This is what I've tried
//<li> <a href='{{path('ecomerce_first',{'id':17})-'.html'}}'> First </a> </li>
ecomerce_first:
path: /{id}/first.{_format }
defaults: { _controller: "EcomerceBundle:Default:firstTest" , _format: html }
requirements: _format: html
id: \d+
in the routing.yml of the EcomerceBundle

As I understood you want have url like this?
/ecomerce/1/first.html
Try next solution:
<li> <a href='{{ path('ecomerce_first',{'id':17, '_format': 'html'})'}}'> First </a> </li>
PS: I noticed that here you have extra whitespace in {_format }
path: /{id}/first.{_format }
try remove it

Related

Route url is including ?=

I'm using the following Razor in my page:
<a asp-action="Logs" asp-route-channel="#channel.Name">
<button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button>
</a>
channel is a string, not an integer, which is why I don't use id.
And this is how I'm routing it:
endpoints.MapControllerRoute("logs", "Dashboard/Logs/{channel}");
And in my Controller, I'm simply doing this for testing purposes:
public IActionResult Logs(string channel)
{
return Content(channel);
}
However, when the link is generated, I get something like this:
<a href="/Dashboard/Logs?channel=mychannel">
Instead of the expected:
<a href="/Dashboard/Logs/mychannel">
EDIT
Here's what I've tried:
[HttpGet("{channel}")]
public IActionResult Logs(string channel)
{
return Content(channel);
}
Which results in https://localhost:44351/mychannel
I've also tried:
[HttpGet("Dashboard/Logs/{channel}")]
public IActionResult Logs(string channel)
{
return Content(channel);
}
Which works as intended, but why should I have to include the whole path like that?
You are mapping to the default convention based route, which would most likely have {id} as a route parameter. Because it maps to the default and you included {channel} it would add that parameter as a query string instead of part of the URL.
You would need to include a custom route for that action in order to get the desired URL generation.
endpoints.MapControllerRoute(
name: "Logs",
pattern: "Dashboard/Logs/{channel}",
defaults: new { controller = "Dashboard", action = "Logs" });
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
and the necessary route values would need to be included so that it maps to the intended route
<a asp-controller="Dashboard" asp-action="Logs" asp-route-channel="#channel.Name">
<button class="btn btn-success btn-circle"><i class="fa fa-eye"></i></button>
</a>

Symfony2 twig path - wrong generated URL behind

I have a problem with generating relative URL in symfony 2.7 project
My project is hosted behind a proxy that redirect URL to the target VM, like this - example.com/oblounge/bo/ to vmhostname/
All my URL are generated in relative
Here is my problem
For example when I'm on this page
http://example.com/oblounge/bo/admin/articles/actualite?section=127
All generated likns are malformated, the "/admin/articles" is duplicated
http://example.com/oblounge/bo/admin/articles/admin/articles/actualite?section=127
Note: The problem is occurred only for rendered controllers
The render action
{{ render(controller('AppBundle:Back/Article/Article:sectionsArticle', { 'rubric': 'actualite' })) }}
The rendered twig
{% for section in sections %}
<li><i class="fa fa-circle-o"></i>{{ section.name }}</li>
{% endfor %}
My routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
The prefix of my admin controller
/**
* #Route("/admin")
*/
class ArticleController extends BaseArticleController
{
I would suggest you give a look at your routing.yml file that is referenced within your bundle.
If you've already defined a prefix for the route in the routing.yml of you app/config there is no need to repeat it in the routing of your bundle , otherwise there will be a duplication of prefixes as in your case.
Example:
This is the routing.yml in the App\config folder
tutoLexikTestBundle_parents:
resource: "#tutoLexikTestBundle/Resources/config/routing/parents.yml"
prefix: /parents
And this is the one of the bundle
parents_show:
path: /{id}/show
defaults: { _controller: "tutoLexikTestBundle:Parents:show" }
I don't need to repeat the prefix 'Parents' in the path

Symfony Twig - get default _locale in twig

I'm adding a language switcher on my website.
my parameters are like this :
avc_coming_soon:
resource: "#AVCComingSoonBundle/Controller/"
type: annotation
prefix: /{_locale}
defaults:
_locale: en
requirements:
_locale: |fr
en is set by default
In my twig, I did that:
<div class="languages">
<ul class="lang-menu">
<li class="en"><img src="{{ asset('images/flag_en.gif') }}" alt="EN"></li>
<li class="fr"><img src="{{ asset('images/flag_fr.gif') }}" alt="FR"></li>
</ul>
</div>
But when I click on 'English', my path become www.mysite.com/en or the good route is www.mysite.com/ (without the /en) because in parameters, I have this :
defaults:
_locale: en
How to get the default _locale in twig ?
{{ path(app.request.get('_route'), {'_locale': <<<default>>> }) }}
thank you :)
You need to use app.request.attributes:
{{ path(app.request.get('_route'), {'_locale': app.request.attributes.get('_locale') }) }}
If you have configuration in controller you need to set default value in function parameter:
/**
* #Route("/", name="coming_soon", options={"expose"=true}, requirements={"_locale" = "fr|en"})
*/
public function indexAction($_locale = 'en')
{
...
}
I use this bundle: https://github.com/schmittjoh/JMSI18nRoutingBundle
Thanks to the bundle I never have to mess with locales..
You can configure the bundle to prefix everything except the default locale (which is what you wan't, reading the comments to the other answer).
And for the language switcher you simply set the _locale on any given route.
when using '%locale%' parameter from service.yaml the value in twig file does not change using subscriber. Using this code works for me:
app.request.session.get('_locale')

Symfony2, how to pass a parameter off route

Id like to know how to pass a parameter off route
route:
frontend_agences_list:
path: /agences/{page}
defaults: { _controller: ProjectFrontendBundle:Frontend:listAgences, page: 1 }
Is it like this ?
twig:
<a href="{{ path('frontend_agences_list') }}?mode=list">
<a href="{{ path('frontend_agences_list') }}?mode=grid">
<a href="{{ path('frontend_agences_list') }}?mode=block">
in fact, I'd like to display results in 3 modes, list , grid and block. In the controller I do a test ,If mode=list so render "list-view.html.twig", elseif mode=grid so render "grid-view.html.twig" ....
Is it the good way to do this or there is another way to do it ?
Twig:
list mode
grid mode
block mode
Your controller:
public function listAgencesAction()
{
$mode = $this->get('request')->get('mode');
if($mode == 'list') {
...
}
}

redirect to page#about using symfony&twig

I'm using Symfony , I wanna redirect the user to a specific frame in a page
So How can i translate this <a href="index.html#about"/> to twig ? I tried <a href="{{path('pl_index')}}#about"/> but it doesnt work
in twig you can use {{path('_welcome') }} and it will send you to your home index, check your route.yml and you can see what are the paths generated by symfony.
for example this is my config.yml:
//savabundle config.yml
sava_inventario_construccion:
path: /productos/construccion
defaults: { _controller: savaInventarioBundle:Inventario:construccion }
sava_inventario_index:
path: /productos/
defaults: { _controller: savaInventarioBundle:Inventario:index }
if i want to generate www.mypage.com/productos/construccion#about link, this is what my html should look like
<a href="{{path('sava_inventario_construccion') }}#about"/>
you can read more in here
http://symfony.com/doc/current/book/templating.html

Resources