Reputation: 156
I am novice at Symfony. Current version : 2.8.16 My problem : I don't manage redirection.
app/config/routing.yml :
gb_platform:
resource: "@GBPlatformBundle/Resources/config/routing.yml"
prefix: /platform
src/GB/PlatformBundle/Ressources/config/routing.yml :
gb_platform_home:
path : /{page}
defaults:
_controller : GBPlatformBundle:Advert:index
page: 1
requirements:
page: \d*
gb_platform_view:
path : /advert/{id}
defaults: { _controller : GBPlatformBundle:Advert:view }
requirements:
id: \d+
src/GB/PlatformBundle/Controller/AdvertController.php :
namespace GB\PlatformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
class AdvertController extends Controller
{
public function indexAction()
{
$url = $this->get('router')->generate(
'gb_platform_view',
array( 'id' => 5)
);
return new Response("L'url de l'annonce d'id 5 est : ". $url);
}
public function viewAction($id)
{
return $this->redirectToRoute('gb_platform_home');
//return new Response("lol");
}
}
The error is the following :
Unable to generate a URL for the named route "gb_platform_home" as such route does not exist.
I can access manually to both route ("app_dev.php/platform/" can be reached). But the URL generation is only working for gb_platform_view.
I supposed the source of the problem could be in app/config/routing.yml file specifying no itteration ... but if I do so it doesn't solve anything and seems to create more problems.
I find the debugging route command line :
php app/console debug:router
Here is the result.
gb_platform_home is clearing identify as a know route so I don't understand. Also, you may notice there's a shifted unique print for this route in the shell. Why ? Key for solving ?
Could a 4 spaces encoding be the problem ?
Thanks for your help.
Upvotes: 2
Views: 3557
Reputation: 529
This solution below should work as you expect.
Requirement
and Default value
will not work as you expect when generating route but rather when parsing the user entered url
your controler will need to be define like this:
// you must set the default value like that `$page = 1`
function indexAction(Request $request, $page = 1)
gb_platform_home:
path : /{page}
defaults:
_controller: GBPlatformBundle:Advert:index
page: 1
requirements:
page: \d*
gb_platform_home_: <------ use it to generate your empty route
path : /
defaults: { _controller: GBPlatformBundle:Advert:index }
Cause of things like that i prefer using route as annotation
/**
* @Route("/platform/{page}", requirements={"page": "\d*"} ,defaults={"page":1})
* @Route("/platform/{page}/", requirements={"page": "\d*"}, defaults={"page":1})
* @Route("/platform", name="gb_platform_home")
*/
function indexAction(Request $request, $page = 1)
{
}
Upvotes: 3