When I try to use 2 optional variables in Symfony2 routing I have th error: No route found for "GET /"
In routing.yml I have:
AcmeAshavatBundle_homepage:
pattern: /{page}/{ads_on_page}/
defaults: { _controller: AcmeAshavatBundle:Page:index, page:1, ads_on_page:2 }
requirements:
_method: GET|POST
And when i go to http://localhost:8080/AshavatSy/web/app_dev.php/ I have the error. The intresting is that if I run http://localhost:8080/AshavatSy/web/app_dev.php/1 it works well.Also, if I change the path to pattern: /main/{page}/{ads_on_page}/ it works well.
What is the problem?
I'd like to ask, that someone will try to do like this [e.g. pattern: /a/b/ defaults: {... a:1,b:2}, or as he thinks you should do it] in his project, and see is it a common problem...
I managed to have something similar working by defining two routes, pointing to the same controller, using default parameters. In my case, using annotations:
/**
* #Route("/products/{catId}/{prodId}", defaults={"catId"="", "prodId"=""})
* #Route("/products/")
* #Template()
*/
public function indexAction($catId = null, $prodId = null) {
...
I think that using default parameters only, Symfony would expect two /.
HTH
I think you forgot to pass these two arguments to your IndexAction() in controller.
Try this code
Public function indexAction($page,$ads_on_page)
{}
Hope this helps you.
Related
i need for Sylius displayAction a requirement like that:
foo_display:
pattern: /{slug}/{stepName}
defaults: { _controller: sylius.controller.process:displayAction, scenarioAlias: 'foo_ordering', _menu_item: foo_order }
requirements:
slug: micropage|standard
But this not work for me. It will be exit with the follow exception:
The "foo_display" route has some missing mandatory parameters ("slug").
500 Internal Server Error - MissingMandatoryParametersException
How i can fix this? I don't want to add the same route for "standard". I will use one for two ;)
Simple question but I have issues with it.
We have simple route
profile_api_info:
pattern: /api/info/{apiID}
defaults: { _controller: SiteProfileBundle:Api:info, apiID: null}
When we use such url as
http://some.site/api/info/123
we'll get proper result of controller.
But when we use this one
http://some.site/api/info/
we'll have an error, why?
No route found for "GET /profile/api/info/"
We'll have already setuped 'defaults' for our 'apiID' but symfony2 says that no route. Can someone suggest how to deal with it? I want routes
http://some.site/api/info
http://some.site/api/info/
have the same controller as
http://some.site/api/info/123
but with 'apiID' = null or false, no matter.
You have two options.
Option 1:
Pass your default parameter.
profile_api_info:
pattern: /api/info/{apiID}
defaults: { _controller: SiteProfileBundle:Api:info, apiID: null}
However you will not be able to have trailing /.
This would be correct: http://some.site/api/info
This would be incorrect http://some.site/api/info/
Option 2:
set up an additional route.
(This would be my preference.)
profile_api_info_woId:
pattern: /api/info/
defaults: { _controller: SiteProfileBundle:Api:info}
In your controller make sure set the default for $apiID to null.
public function infoAction($apiID = null){...}
Using two routes with one controller method should work for all of the following urls:
http://some.site/api/info
http://some.site/api/info/
http://some.site/api/info/123
I've ran into this multiple times and clearing the cache so that Symfony can rebuild the route definitions usually fixes the issue.
Syntax and everything looks correct. However, the route with the trailing slash (http://some.site/api/info/) won't work but http://some.site/api/info should.
When dealing with the Sylius e-commerce bundles I have found what seems to be a way of configurig the template for a route, that I didn't know:
I have tested in a fresh Symfony RC 2.2.0 with vendors installation.
This would be in the routing.yml
_welcome:
pattern: /
defaults:
_controller: AcmeDemoBundle:Welcome:index
_template: AcmeDemoBundle:Welcome:index # added by me
this generates an error:
FatalErrorException: Error: Call to a member function getTemplate() on
a non-object in
.... \vendor\sensio\framework-extra-bundle\Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener.php
line 62
now, in TemplateListener, what we have is:
if (!$configuration = $request->attributes->get('_template')) {
return;
}
if (!$configuration->getTemplate()) {
$guesser = $this->container->get('sensio_framework_extra.view.guesser');
$configuration->setTemplate($guesser->guessTemplateName($controller, $request, $configuration->getEngine()));
}
$configuration is a String, actually the template I put in routing.yml (AcmeDemoBundle:Welcome:index). Checked by adding a var_dump and also inspecting ParameterBag -> get method which is what $request->attributes is.
So. Why TemplateListener is expecting an object? What am I missing? Am I missconfiguring in routing.yml?
This parameter is not available in Symfony itself.
Feature is provided by SyliusResourceBundle and available only in Sylius controllers.
And apparently _template request attribute conflicts with SensioFrameworkExtraBundle, which uses same name to store object.
We have to move those parameters one config node deeper, to avoid such problems in future.
You can keep an eye on the https://github.com/Sylius/SyliusResourceBundle repository, fix should arrive today.
I'm trying to have a route in symfony that matches the following url:
/filename/version/
where filename is an image, say "image.png" and version as say "foo".
MyImageBundle_resize:
pattern: /{filename}/{sizename}/
defaults: { _controller: MyImageBundle:ImageResize:resize }
and this matchs the pattern /myfile/XXL/, for example. But when I use myfile.jpg, as in /myfile.jpg/XXL/ Symfony seems to break on the period, and returns 404. I found this article which suggests that everything except the "/" will match (which doesn't make any sense if the period is breaking here).
Is there a way I can have the route match on the period?
You don't need to allow slash in URL. Just use simple route:
file_check:
pattern: /check/{filename}/{version}
defaults: { _controller: AcmeDemoBundle:File:check }
And in your controller use:
public function checkAction($filename, $version)
{
//some action
}
I am trying to start on Symfony2 but ran into a problem right away following the Symfony 2 "the book" part "Creating pages in Symfony 2":
I did this:
Created the bundle
php app/console init:bundle "Acme\StudyBundle" src
*Added the namespace in app/autoload.php *
$loader->registerNamespaces(array(
'Acme' => __DIR__.'/../src',
));
Initialized the bundle
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Acme\StudyBundle\AcmeStudyBundle(),
);
// ...
return $bundles;
}
Created the routes in app/config.routing.yml and src/Acme/StudyBundle/Resources/config/routing.yml
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: FrameworkBundle:Default:index }
hello:
resource: "#AcmeStudyBundle/Resources/config/routing.yml"
# src/Acme/StudyBundle/Resources/config/routing.yml
hello:
pattern: /hello/{name}
defaults: { _controller: AcmeStudyBundle:Hello:index }
Created the controller
// src/Acme/StudyBundle/Controller/HelloController.php
namespace Acme\StudyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
When I load the page: http://localhost/app_dev.php/hello/Ryan Symfony gives me an exception:
Unable to find controller "AcmeStudyBundle:Hello" - class "Acme\StudyBundle\Controller\HelloController" does not exist.
I got over the code several times but cannot find anything wrong.
just add
<?php
in the beginning of your controller file : src/Acme/StudyBundle/Controller/HelloController.php
it's solved the problem to me.
Afaik there is a discussion going on within the Symfony 2.0 dev guys in what places they should keep the "Bundles" extension.
I've just grabbed the latest version of Symfony via Git and followed your code 1:1.
I got various error messages too but when I changed...
in src/Acme/StudyBundle/Resources/config/routing.yml
defaults: { _controller: AcmeStudyBundle:Hello:index }
to
defaults: { _controller: AcmeStudy:Hello:index }
app/config/routing.xml
resource: "#AcmeStudyBundle/Resources/config/routing.yml"
to
resource: "#AcmeStudy/Resources/config/routing.yml"
...i got a pretty "Hello Ryan" in the browser.
Hope this helps!
You are probably running PR9. Update to PR11(latest), and I would bet this gets resolved. Symfony devs removed the 'Bundle' suffix in PR9, but added it back again shortly there after.
Also, Symfony devs keep an Update log that I find extremely helpful.