I'm going to create a modules system in my Symfony 2 app. Each module will be a bundle.
I don't know how to I can dynamically (in my service code) load routes from file (eg. AcmeSomeModuleBundle/Resources/config/routing.yml) and apply them with some prefix (or host). Like it's done by embedding code below in app/config/routing.yml:
berg_applications:
resource: "#AcmeSomeModuleBundle/Resources/config/routing.yml"
host: foobar.com
Any solutions?
You need custom route loader IMO: http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html
For one project, I had to build route loader which loaded routes by fetching them from remote URL via CURL and it worked perfectly.
Documentation is very clear and it's silly easy to build one yourself when you look at the example. Basically, key things are:
"type" when you're defining a route resource. You should make your custom type so that your route loader recognizes it and takes it for processing.
::load() method.
If you have any concrete problems you stumble upon don't hesitate to post question in comment. Basically, your RouteLoader will receive "resource" in it's load method and should do whatever it needs to do with it to add new Route to Router.
If you do a true bundle approach for each module, then the easiest way to accomplish what your trying to do is use the JMS Security-Extra bundle with attribute-based routing.
To your composer.json file, add this:
"require": {
...
"jms/security-extra-bundle": "1.5.*",
Update your composer file with
php composer.phar update
Then in your BundleName/Resources/config/routing.yml file do this:
some_name:
type: annotation
resource: "#SomeBundle/Controller"
Finally, for each action in your controller, decorate it with #Route attributes:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
/**
* #Route("/SomeBundle/SomeController")
*/
class SomeController extends Controller {
/**
* #Route("someAction", name="myAction")
* #Method("GET") OR
* #Method({"GET", "POST"})
*/
public function someAction() {
}
}
Some of the other attributes in the JMS bundle make things really nice as well. For example, I use the #Template attribute on my actions quite a bit. This means that I no longer have to do:
public function recentListAction() {
...
return $this->render(
'AcmeArticleBundle:Article:recentList.html.twig',
array('articles' => $articles)
);
}
I can simply do:
/**
* #Route("/Articles/List")
* #Template()
*/
public function recentListAction() {
...
return array('articles' => $articles);
}
And as long as I have a Resources/views/ControllerName/recentList.html.twig file, everything will be weaved together for me automatically.
Related
i'm new to Symfony. I'm trying to create dynamic universal route that will pick required controller based on part of url. I've found in docs that there is special param {_controller} that can be used in route pattern, but could not find any examples of usage.
// config/routes.yaml
api:
path: /api/{_controller}
So for example for route /api/product i expect ProductController to be initiated.
But as a result i get error "The controller for URI "/api/product" is not callable: Controller "product" does neither exist as service nor as class."
Can somebody please help me understanding how {_controller] param works? Or maybe there is a better way for specifying universal route that can dynamically chose controller without listing controller names in routes.yaml.
Thanks in advance
This isn't really the cleanest way to do what I think you're trying to do. If I am correct in assuming you want to have a /api/product/ point to methods in your product controller, then something like this is more "symfonyish"
// src/Controller/ProductController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/api/product", name="product_")
*/
class ProductController extends AbstractController
{
/**
* #Route("/", name="index")
* --Resolves to /api/product/
*/
public function index(): Response
{
// ...
}
/**
* #Route("/list", name="list")
* --Resolves to /api/product/list
*/
public function list(): Response
{
// ...
}
/**
* #Route("/{productID}", name="get")
* --Resolves to /api/product/{productID}
*/
public function get(string $productID): Response
{
// $productID contains the string from the url
// ...
}
}
Note that this really just scratches the surface of Symfony routing. You can also specify things like methods={"POST"} on the routing directive; so you could have the same path do different things depending on the type of request (e.g. you could have a route /product/{productID} that GETs the product on that request but updates the product on a PATCH request.
Regardless, the takeaway here is that it is unwieldy to have all of your routes defined in routes.yml rather you should define your routes as directives in the controller itself.
I have controller where I have two actions: createAction and showAction.
createAction creates form from form class and renders it to index.html.twig.
showAction makes database query and takes there some data and renders it to index.html.twig (same .twig file as before).
How I can have two actions in one route? I tried to do two same routes but different name in routing.yml, but it doesn't work. It only renders the first one.
(Sorry, bad english)
You can have the same URL for two separate actions as long as they respond to different http verbs (POST/GET/PUT/etc). Otherwise how would you expect the router to decide which action to choose?
Learn how to define http method requirements from the Adding HTTP Method Requirements section of the Routing documentation.
An example of annotation configuration:
class GuestbookController
{
/**
* #Route("/guestbook")
* #Method("POST")
*/
public function createAction()
{
}
/**
* #Route("/guestbook")
* #Method("GET")
*/
public function showAction()
{
}
}
I have problem with path to twig file. I would like to shorten the path. Look at my example:
class DefaultController extends Controller
{
public function indexAction($name)
{
return $this->render('CatalogFrontendBundle:Default:index.html.twig', array('name' => $name));
}
}
Of course it will work, but look at the path to twig file it's so long.. I now that I could use annotation like this #Template() but I don't want to. Is there any other way to use default twig file to render my page?
Default twig file - I understand as the file whose name is the same like name of action method. So if name of action is indexAction the name of twig file should be index.html.twig.
If you'd like a Symfony experience that is more geared toward Rapid Application Development (RAD) take a look at the KNP RAD bundle. This gives you a lot of convention-based shortcuts to work with, including automatic template resolution. See here: http://rad.knplabs.com/#template
I can't found better solution to my problem. So I decide to use #Template, Mark have right it will be cached. And I think that better option is to use those annotation than write my own method or put all of the path in the render method.
So the answer is below:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
*
* #Template()
*/
public function indexAction($name)
{
return array('name' => $name);
}
}
In Symfony is it possible to access a controller through a route that potentially can contain indefinite number of slug, taking advantage from internal routing system?
e.g.
my_route:
pattern: /{slug_parent}/{slug_child}/{slug_nephew}/{slug_...}/...
as
www.mydomain.com/math/arithmetic/fractions
but also
www.mydomain.com/tech/android
It can be done, but instead of creating routes in config YML I would recommend to use #Route annotations. Add annotations in the controller class, like this:
/**
* #Route("/{slug_parent}/{slug_child}")
* #Route("/{slug_parent}/{slug_child}/{slug_nephew}/")
* #Route("/{slug_parent}/{slug_child}/{slug_nephew}/{slug_...}/")
*/
public function yourControllerAction()
{
...
}
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
I just downloaded symfony2 and am beginning to play with routing via annotations. I have my app/config/routing.yml set in the bundle I created to use annotations, and have deleted the Acme bundle and all routing references to it. That said, I've tried creating a couple different route annotations in my controller like #Route("/") and #Route("/hello/{name}") but I'm always greeted with a 404 error (using the dev environment). If I add the route in routing.yml it works just fine even though the routing is configured to use annotations. For whatever reason, my annotations are seemingly being ignored.
Here is my app/config/routing.yml:
DanDefaultBundle:
resource: "#DanDefaultBundle/Controller/"
type: annotation
prefix: /
And here is my controller method:
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
return array('name' => 123);
}
I've included the Sensio\Bundle\FrameworkExtraBundle\Configuration\Route namespace - everything as far as I can tell is correct with what I've seen in the documentation. What am I overlooking that's causing symfony2 to seemingly ignore my routing annotations? Again, if I add the routes to the routing yaml everything works so my bundle is working - but annotations seem to be ignored.
Thanks!
Dan
UPDATE:
It looks like I had to add the routes to routing_dev.yml in addition to routing.yml since I was operating in the dev environment. I suppose that's so you have have different routes in-between development and production? I suppose special care will have to be taken to make sure those routes stay in sync?
You accidentally removed the inclusion of routing.yml from routing_dev.yml.
if you use Route Prefix in your routing.yml
you must declare about your prefix above your class declaration like that :
/**
* #Route("/")
*/
class PostController extends Controller
{
/**
* #Route("/")
* #Template()
*/
public function indexAction()
{
}
/**
* #Route("{id}")
* #Template()
*/
public function showAction($id)
{
}
}
Like into Sensio FrameworkExtra Bundle Documentation