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();
}
....
Related
I want to override the tird-party bundle's controller in Symfony4.
in this explanation.
It says.
If the controller is a service, see the next section on how to override it. Otherwise, define a new route + controller with the same path associated to the controller you want to override (and make sure that the new route is loaded before the bundle one).
What I want to do is to override this Controller.
myapp/vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php
Now I make CRUDController.php in
myapp/src/Controller/CRUDController.php
It doesn't work,, maybe of course though.
However, what should I do next???
Thanks to #DreamOn comment
I made the file myapp/src/Controller/CRUDController.php
<?php
namespace App\Controller;
use Sonata\AdminBundle\Controller\CRUDController as BaseCRUDController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class CRUDController extends BaseCRUDController{
}
But, it doesn't work, I guess I should do some routing setting.
Normally SonataAdmonBundle is routed like this.
routing.yml
admin_area:
resource: "#SonataAdminBundle/Resources/config/routing/sonata_admin.xml"
prefix: /admin
_sonata_admin:
resource: .
type: sonata_admin
prefix: /admin
However Symfony4/3.4 recommend annotation routing.
It makes me confused.
How can I register/routing my Controller?
I use this instead, not override.
Extend bundle controller. Add bundle controller as a base controller by:
use Sonata\AdminBundle\Controller\CRUDController as BaseCRUDController;
And make your CRUDController extends BaseCRUDController
I'd like all my Controller action routes in the namespace 'App\Controller\Api' to have the prefix '/api'. I also want to use annotations inside the controllers to set the rest of the route.
In Symfony 3 this was done by editing 'config/routing.yml':
app:
resource: '#AppBundle/Controller/Api'
type: annotation
prefix: /api
How can I do this in Symfony 4? Do I need to make a bundle? Which config file would I use since I don't have a 'config/routing.yml'?
Ok It looks like I should have just tried real path names. The following worked in 'config/routes.yaml':
api:
prefix: /api
resource: '../src/Controller/Api'
At first run composer require annotations and then
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/blog")
*/
class BlogController extends Controller
{
}
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 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.
hi i have a problem with my routing in symfony. my routing_dev looks like:
_test:
resource: "#MyBundle/Controller/DefaultController.php"
type: annotation
prefix: /test
then i have a controller:
/**
* #Route("/", name="_test")
* #Template()
*/
public function indexAction($name)
{
return array('name' => $name);
}
and when i try to run the /test i get the message:
No route found for "GET /test"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
i dont know where the error is. i need your help. how to find the problem?
EDIT: I'm working with the app_dev.php not app.php
First, you must be sure that routing.yml import the file routing_dev.yml.
Then, you must define the $name variable passed in argument in the route. If this argument is optional, you can give it a default value.
Moreover, note that you can put the prefix directly in the annotation.
/**
* #Route("/blog")
*/
class TheClassController extends Controller
{
/**
* #Route("/{name}", name="_test", defaults={"name" = "Jean"})
* #Template()
*/
public function indexAction($name)
{
return array('name'=>$name);
}
}
After, you can run run the command php app/console router:debug for test the route.
Maybe you have to use http://your.site/app_dev.php/test url. Or copy your routing information to routing.yml (not routing_dev.yml)
Are you actually typing into your url /test? Because your route is just "/", the name you have given is used to generate that route from a template. If you want the url to be /test you should change your route parameter to #Route("/test", name="test")
you explicit have to set the route for an action. not just for the whole controller.
your routing.yml should look like:
mybundle_controller:
resource: "#MyBundle/Controller"
type: annotation
prefix: /test
your controller like:
/**
* #Route("/") #Note you can omit this, since you set the prefix in the routing.yml
*
*/
public function indexAction($name)
{
/**
* #Route("/", name="_test"
* #Template()
*/
return array('name' => $name);
}
try to run php app/console router:debug
it should return you something like
_test ANY /test/
If you are using Symfony 4 you just need to install doctrine/annotations package. To download it, in your terminal run:
composer require annotations
The command will download the package into your vendor folder and add new annotations.yaml file under config/routes folder. Make sure annotations.yaml has following lines:
controllers:
resource: ../../src/Controller/
type: annotation
You do not need any additional route settings inside config/routes.yaml file.