I have create new bundle in src\Moda\CategoryBundle\Controller\DefaultController.php
and a change routing to:
namespace Moda\CategoryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* #Route("/show", name="_show")
* #Template()
*/
public function indexAction()
{
die('test');
return array();
}
}
and my routing.yml in app/config
moda_category:
resource: "#ModaCategoryBundle/Controller/"
type: annotation
prefix: /
This links dosnt work:
localhost/web/app_dev.php/category/show
localhost/web/app_dev.php/show
Do you know what I am doing wrong?
I think you should import the config.yml file inside your bundle.
So instead of :
moda_category:
resource: "#ModaCategoryBundle/Controller/"
type: annotation
prefix: /
Change it to:
moda_category:
resource: "#ModaCategoryBundle/Resources/config/routing.yml"
type: annotation
prefix: /
And then add the routes you need inside that file.
Related
I have added a second bundle and when I try to open some url from that bundle, I keep getting an error that the route was not found. Adding the same route to the main bundle works perfectly.
What's wrong?
This is my project structure. I also:
- added the UserBundle to AppKernel.php (IDE shows the class exists)
- use AppBundle\Controller namespace in AppBundle, and UserBundle\Controller namespace in UserBundle
The controller I try to access from the UserBundle looks like this:
namespace UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* #Route("/login", name="user_login")
*/
public function loginAction(Request $request)
{
return array();
}
}
I suspect you need to add the UserBundle to your routing.yml configuration. You should have something like this in the routing.yml:
user_bundle:
resource: "#UserBundle/Controller/"
type: annotation
// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I'm new of Symfony framework. I tried this simple code without any result.
That's the server response:
No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.
I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml.
# app/config/routing.yml
lucky:
resource: "#AppBundle/Controller/LuckyController.php"
type: annotation
See Symfony docs for more details
Also try clearing the cache, just to be sure.
You're missing an important part of the route and its name:
/**
* #Route("/lucky/number", name="lucky_number")
*/
More information: http://symfony.com/doc/current/book/routing.html
So basically I have a controller of a bundle in which i want to and a route prefix so i use the #Route annotation on the class, i've done this all of the other controllers of my Symfony2 app. However this one does not take the prefix into account so instead of being able to access the page on /admin/users/list i only access it on /list.
Here is the controller :
<?php
namespace LanPartyOrg\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use JMS\SecurityExtraBundle\Annotation\PreAuthorize;
/*
* #Route("/admin")
*
*/
class AdminController extends Controller
{
/**
* #Route("/list", name="users_list")
* #Template("LanPartyOrgUserBundle:Admin:List.html.twig")
*/
public function listAction(){
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('LanPartyOrgUserBundle:User')->findAll();
return array('users'=>$users);
}
}
And here is my routing.yml :
lan_party_org_user:
resource: "#LanPartyOrgUserBundle/Controller/"
type: annotation
prefix: /
fos_user_security:
resource: "#FOSUserBundle/Resources/config/routing/security.xml"
fos_user_profile:
resource: "#FOSUserBundle/Resources/config/routing/profile.xml"
prefix: /profile
fos_user_register:
resource: "#FOSUserBundle/Resources/config/routing/registration.xml"
prefix: /register
fos_user_resetting:
resource: "#FOSUserBundle/Resources/config/routing/resetting.xml"
prefix: /resetting
fos_user_change_password:
resource: "#FOSUserBundle/Resources/config/routing/change_password.xml"
prefix: /profile
Thanks for any help
Annotations must be added to docblocks, not just simple comments.
You need to start your comment with /** instead of /* (notice double *):
/**
* #Route("/admin")
*/
class AdminController extends Controller
{
// ...
}
This is going to prefix all your AdminController's routes with /admin.
Your following code
/*
* #Route("/admin")
*
*/
should be as
/**
* #Route("/admin")
*
*/
and also make sure that two controller should not have same prefix .
I need some help because I didn't succeed in finding the source of my route problem.
Here is my routing.yml:
gstyle39VideothequeBundle:
resource: "#gstyle39VideothequeBundle/Resources/config/routing.yml"
prefix: /videotheque
The routing.yml in VideothequeBundle/Resources/config/:
VideothequeBundle_homepage:
pattern: /
defaults: { _controller: gstyle39VideothequeBundle:Videotheque:index }
My controller:
<?php
namespace gstyle39\VideothequeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
class VideothequeController extends Controller
{
public function indexAction()
{
return new Response("Kikoo");
}
}
For my part I did a "cache:clear", a "router:debug" which detected my route:
VideothequeBundle_homepage ANY /videotheque/
I also manually deleted the folder app/cache...
IMHO you didn't add routing definition in application global routing.yml (app/config/routing.yml).
Look at my routing definitions in https://github.com/drupalmk/Jobeeto
I am using annotations to define routes in controllers and I have 15 controllers. All are executed by /path1 , /path2.
Is there any way that in all those controller , I can access them via /admin/path1 and /admin/path2?
I don't want to enter that by changing each file.
Can I do that from a single location? I mean the whole bundle should open via /admin and then their respective paths.
try this
# app/config/routing.yml
acme_hello:
resource: "#AcmeHelloBundle/Resources/config/routing.yml"
prefix: /admin
or if using annotations
resource: "#AcmeHelloBundle/Controller"
type: annotation
prefix: /admin
Use this in routing.yml:
Admin:
resource: "#AdminBundle/Controller"
type: annotation
prefix: /admin
Just define the annotation for your Class (not for method)
/**
* #Route("/blog")
*/
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix
If you want to prefix specific controller DevController for example and have something like:
myproject.com/dev/test
in your Controller add the following Route annotation as in example:
/**
* #Route("/dev")
*/
class DevController extends Controller{
/**
* #Route("/test")
*/
public function testSavingAction(){
return new Response();
}
....