I've set up my entities, now I want to
use the doctrine:generate:crud, during this command it asks what route
prefix I would like. I would expect that this means that the routes
would automatically be generate, this is not happening. So I need to
know if it is supposed to generate the routes, or if I'm supposed to
create them all manually? If it is the case that I need to generate
them manually is there a route class, to define all the routes for the
CRUD operations?
When you generate a CRUD with Symfony, it will ask you to choose a configuration format.
By default, it's annotation. If you haven't changed it, then your routes are in the entity controller, as annotation.
In the example below, you can see the #Route anotation, which is how to define the URL in anotation.
/**
* Finds and displays a user entity.
*
* #Route("/user/{id}", name="user_show")
* #Method("GET")
*
* #param User $user
* #return \Symfony\Component\HttpFoundation\Response
*/
public function showAction(User $user) {
$deleteForm=$this->createDeleteForm($user);
return $this->render('security/show.html.twig', array(
'security'=>$user,
'delete_form'=>$deleteForm->createView(),
));
}
In the end, it's not that "It didn't happen", it's simply and most likely that you haven't read some doc, and didn't knew about it... ;)
Symfony doc: Routing
Related
this is my first time of developing with translation service.
I implemented it, seems to be working, but how can I test other languages?
Can I change the default language in my symfony project?
Or can I change the language transfered to my project via the browser?
(I only found the settings to change browser GUI language)
Regards
n00n
you can also do it inside controller using the session like this:
/**
* Switch language
*
* #Route("/switchLanguage/{locale}/", name="switch_language")
*
* #param Request $request
* #param string $locale
*
* #return RedirectResponse
*/
public function switchLanguageAction(Request $request, $locale): RedirectResponse
{
$request->attributes->set('_locale', null);
$this->get('session')->set('_locale', $locale);
return $this->redirect($request->headers->get('referer'));
}
Yes of course you can there is few approach for translation.
If you want save your project translation data in your database you must create entities for data translation and after that you can use global sql filter for selecting data each language.
And you can read about Symfony Translation Component.
For changing project language you can write listener.
like this
public function onKernelRequest(GetResponseEvent $event)
{
/** Set language parameter*/
$lang = $event->getRequest()->query->get('lang', 'en'); //this is optional you can write another code for getting language.
$event->getRequest()->setLocale($lang);
}
This example if you want to set each request language.
Read symfony translation component documentation there are more useful things.
Is there anything in the Symfony annotations modules that allow me to use them for other uses?
I know for #Route and #Method you need to extend existing libraries, so its just not that easy i'm guessing.
Currently, i'm working with the JS History API, and would LOVE to put the popState data for my JS files in the annotations. So they are already available when the routing generates the URL.
Q Doesn't this makes sense to have a, HTML5 annotated title, or some attribute here? It would be great to have the ability to define this data, as annotated, right next to the already existing route name and stuff.
Q Is there anybody that has tweaked with the annotations before?
I wanted to clarify my intentions here as I think I left out some crucial details (the mention of History API) for understanding my use case.
There is a few SPA front ends that have been integrated through a front-end bundle, and this connected via AJAX calls to a backend bundle which was a straight RESTful API, with the addition of a very fun-to-develop PHP API class I made that intereprets and processes (routes) the AJAX in a fashion that directly executes other PHP class controller `methods.
I use a lot of ajax for this Symfony 2 app (fosjsrouter) to handle routing. So instead of URLs triggering the routes and actions, the SPA click event fires off AJAX to the back end router, with a large JSON payload, not limited to PHP control parameter's (class/method/var names), and data sets.
OK, so getting back on track; Given the above scenario; In the JS class object end of the router, inside this I thought it was the best place to add some JS History API functionality to it, (state, back button, etc.)
The above class can be called if a history flag was called, which could become responsible for assigning initial state data. Primarily, this is because the JSON data object that's being around in this JS method contains already a lot of the crucial route data, and param information for that route needed in the backend PHP, which comes from the annotations.
So the idea is if I add accessibility for a history state title and URL to the annotations, then I will have access to that information right there available to define the initial state, if flagged, right inside the an ajax.done(), inside this main JS routing method.
Now we can pass state back and forth two ways between the db and realtime client-side async. You can use an observer, or anything, from there front-end, and jobs/queues on the backend to keep it fully reactive. (use React too :-))
EDIT I'm not so sure that's what I was thinking, it looks like its making me set the values of the title and url for this inside the return statement of the PHP function, where I want it set in the annotation (see return 'Matthias Noback';)
So I'm trying this, but where do I set these titles at?
<?php
namespace Blah\CoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* #Annotation
*/
class HistoryAnnotationController
{
//history state params are out properties here..
/**
* #var
*/
private $url;
/**
* #var
*/
private $title;
/**
*
*/
public function __construct()
{
}
/**
* #return mixed
*/
public function getTitle()
{
return $this->title;
}
/**
* #return mixed
*/
public function getUrl()
{
return $this->url;
}
}
I want to set it WAY back here, so the ajax that calls this route has access to it.. (look for #historyApiTitle in this code, etc..)
<?php
namespace Blah\Bundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpFoundation\JsonResponse,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Method,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Route,
Sensio\Bundle\FrameworkExtraBundle\Configuration\Template,
Blah\Bundle\Entity\Test,
Doctrine\ORM\Query; //for hydration
class StuffController
{
/**
* #Route("/some/route/name/{test}", name="some_route_name", options={"expose"=true})
* #param $test
* #return mixed
* #historyApiTitle('This is the get something page')
* #historyApiUrl('/get_something')
*/
public function getSomethingAction($test)
{
$em = $this->getDoctrine()->getManager();
$dql = "
SELECT s
FROM BlahBundle:Stuff s
WHERE s.test = :test";
$query = $em->createQuery($dql);
$query->setParameter('test', $test);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,
$this->get('request')->query->get('page', 1), 1000);
return $this->render('BlahBundle:Stuff:get_something.html.twig', array('pagination' => $pagination));
}
}
Q So looking at these TWO code examples, how do I connect the dots between the two to get this to work?
Yes you can annotations classes you can follow the following tutorial Creating Custom annotations Classes
Basic rules are the follows:
Your class should have the #Annotation -phpdoc comment
/**
* #Annotation
*/
class CustomAnnotation
{
public function __construct($options) {}
}
In Your Needed class just use it in standard way;
class Person
{
/**
* #CustomAnnotation("option")
*/
public function getName()
{
return 'some stuff';
}
}
You should looks at the AOPBundle, it allows you to do treatement from your personnals annotations. But I don't thinks trying to do annotations in the view is a good idea. You need to parse the javascript with php, and it sounds bad.
I have just started using Symfony and I am having a routing problem. Here is the routing fromt the controller:
/**
* #Route("/social/{name}/", name="_speed1")
* #Route("/social/drivers/")
* #Route("/social/drivers/{name}/", name="_driver")
* #Route("/social/", name="_speed")
* #Template()
*/
public function unlimitedAction()
{
If I go to speed/social/ or speed/social/bob or speed/social/drivers/ or speed/social/drivers/bob all of those pages render with no problem. However I need the name being passed in so I changed
public function unlimitedAction()
{
to
public function unlimitedAction($name)
{
If I go to speed/social/drivers/ or speed/social/drivers/bob it returns fine. However, if I go to speed/social/ then I get the following error:
Controller "MyBundle\Controller\DefaultController::unlimitedAction()"
requires that you provide a value for the "$name" argument (because there is
no default value or because there is a non optional argument after this one).
I can't understand why it works for one route but not the other.
So my question is, how can I acheive my routing so that I can go to:
speed/social/
speed/social/drivers/
speed/social/drivers/bob
And be able to pass the variable to the action without error.
Thanks!
To answer your question: you have to provide a default value for name parameter, for each route without the {name} parameter in the url. I can't test it right now and I can't remember the syntax when using annotations, but should be something like this:
/**
* #Route("/social/{name}/", name="_speed1", defaults={"name"=null})
* #Route("/social/drivers/{name}/", name="_driver", defaults={"name"=null})
* #Template()
*/
public function unlimitedAction($name)
{
}
This way you should be able to call /social/ and /social/foo as well as /social/drivers/ and /social/drivers/foo.
But, really, this is not the right way to go. Just define more actions, each binded to a single route:
/**
* #Route("/social", name="social_index")
* #Template()
*/
public function socialIndexAction() { } // /social
/**
* #Route("/social/{name}", name="social_show")
* #Template()
*/
public function socialShowAction($name) { } // /social/foo
As a general rule, each method (each action) should be focused to do just one thing and should be as short as possible. Use services and make your controllers do what they are supposed to do: understand user input, call services and show views.
I have a tree of Employee objects (they are in a tree-like hierarchy, with everyone having one leader, and all leaders having more employees). All the Employees have a integer parameter called units.
/**
* #ORM\Entity
* #ORM\Table(name="employees")
*/
class Employee
{
/**
* #ORM\Id
* #ORM\Column(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="Employee", mappedBy="leader")
*/
protected $employees;
/**
* #ORM\ManyToOne(targetEntity("Employee", inversedBy="employees")
*/
protected $leader;
}
I need to get all the employees, who have at most N units, where N is defined in config.yml. At first, I was trying to push $configContainer into $GLOBALS, and use it in ArrayCollection::filter()'s Closure. Now I found a method, so I can use variables in the Closure:
public function getBestEmployees(&$configContainer)
{
return $this->getAllEmployees()->filter(
function bestEmployees($employee) use ($configContainer)
{
return ($employee->getUnits() >= $configContainer->getParameter('best_unit_count'));
}
);
}
Now I wonder if there is any other way to access the configuration parameters from an Entity, or do I really have to pass the whole configContainer as a reference? Or am I doing it totally wrong?
You shouldn't be accessing the service container at all inside entities. The value itself should be passed instead
public function getBestEmployees($bestUnitCount)
{
return $this->getAllEmployees()->filter(function ($employee) use ($bestUnitCount) {
return $employee->getUnits()->count() >= $bestUnitCount;
});
}
Of course, we haven't actually solved the problem yet: the parameter still needs to be fetched from the container somewhere. If this method gets invoked mostly in controller actions, I wouldn't bother doing any extra work to make things cleaner and would pass the container parameter straight in the controller action.
However, should there be a need to get the best employees in a Twig template, for example, it would be nice if it wouldn't be necessary to pass the parameter. One possibility would be using a setter method and passing the parameter down beforehand to each and every entity that gets retrieved from the database. You could do this either in repositories or entitiy managers. The most advanced solution would be to listen to the postLoad event and pass the parameter in an event listener.
I have the following Symfony controller:
/**
* Says thanks to the user for signing up.
*
* #Route("/thanks", name="user")
* #Template()
*/
public function thanksAction()
{
return $this->render('VNNPressboxBundle:User:thanks.html.twig');
}
If I don't include the return statement, I get an error saying the controller must return a response. It's interesting that I have to manually specify which template my action needs to use, considering Symfony could easily figure that out based on my controller and action. Plus that's how Symfony 1.x worked.
I have to imagine that I'm missing something. It doesn't seem like they would apply the convention over configuration concept in Symfony 1.x and then abandon it in Symfony >= 2.0.
Is it possible to tell Symfony to figure out which template to use based on my controller and action, and if so, how?
You have to return something. You're using #Template annotation so you don't have to render the response but you still have to return an array of parameters for the template (in your case empty):
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* Says thanks to the user for signing up.
*
* #Route("/thanks", name="user")
* #Template()
*/
public function thanksAction()
{
return array();
}
Read more on #Template annotation in the docs: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/view.html
P.S. Don't compare symfony 1.x to Symfony 2.x. These are two different frameworks. Symfony 2 favors being explicit over magic.
Return an array. In your case it'll be an empty array, but normally you would fill it with variables you want to pass to a template.