Symfony2: #Template annotation and PHP templates - php

When I use Symfony, I name my views with the name of the corresponding action, so that I can use a #Template annotation without any parameters to render the view.
/**
* #Route("/{id}/show", name="entity_show")
* #Template()
*/
public function showAction($id) {
}
Does this only work with Twig? Is there a way to not having to specify the name of the view as a parameter for #Template when I use PHP views?

In addition to what Patt wrote, you must specifiy the engine for each use of the annotation:
#Template(engine="php")

Related

Dynamic Prefix in Symfony4 Controller Route Annotation

I want to achieve this annotation in my controller, but i can not find any documentation or a way to have a wildcard(*) prefix in my annotation inside the controller.
/**
* #Route("/project/*/{Alias}", name="front-story-page" )
*/
public function ShowStoryFront(Story $story)
{
..
}
I tried a whole bunch of different ways but nothing seems to work!
have you simply tried to add another param ?
/**
* #Route("/project/{WildCardParam}/{Alias}", name="front-story-page" )
*/

Symfony Routing not making sense

I'm teaching myself Symfony. And the routing doesn't make any sense.
I have a postController class with a few actions. Originally the crud generator from the command line gave me this;
/**
* Post controller.
*
* #Route("/post")
*/
class PostController extends Controller
{
/**
* Lists all Post entities.
*
* #Route("/", name="post_index")
* #Method("GET")
*/
public function indexAction()
{
//
}
//
}
What I want to achieve is to remove the #Route from the class itself. Thus I want my indexAction to be the the homepage, and all other actions in my class to still start with /post. For example, this is what I want;
class PostController extends Controller
{
/**
* Lists all Post entities.
*
* #Route("/", name="homepage")
* #Method("GET")
*/
public function indexAction()
{
//
}
/**
* Finds and displays a Post entity.
*
* #Route("post/{id}", name="post_show")
* #Method("GET")
*/
public function showAction(Post $post)
{
//
}
// what I want for the showAction should count for all other Actions as well
}
When I make the change I get an error;
No route found for "GET /post/"
Can somebody please explain to me what I'm doing wrong and how to fix this. I don't think it is something major, it's probably something small that I just don't see. I want to make that indexAction my main action, the action when the website opens after a user logged in. Thank you
Your route specification requires {id} parameter which is obligatory so there's no "GET /post/" route indeed. You need to do one of the following
Pass id value and access /post/1 for example
Remove {id} from route specification and then you can access /post/
Pass default value for id so you can access both /post/ and /post/1. To do that your route specification should look like #Route("post/{id}", name="post_show", defaults={"id" = 1})
No route found for "/post/" is correct
because there is no route "/post/"
in your example theres only "/" and "/post/{id}"
so it makes perfect sense, i prefer to use the yml notation and prefixes so its not bound to a class
check the "YAML" tabs in the DOCS

Symfony3: is it possible to change the name of a form?

With Symfony 2.7, you could customize a form's name in your EntityType class with the method getName()
This is now deprecated. Is there another way to do that with Symfony 3.0 ?
I have custom prototype entry_rows for collections that I would need to use in different forms.
Since the name of the rows is based on the form's name, I would need to change the later in order to use them with a different form.
You should implements the getBlockPrefix method instead of getName as described in the migration guide here.
As example:
/**
* Returns the prefix of the template block name for this type.
*
* The block prefix defaults to the underscored short class name with
* the "Type" suffix removed (e.g. "UserProfileType" => "user_profile").
*
* #return string The prefix of the template block name
*/
public function getBlockPrefix()
{
return "form_name";
}
Hope this help
Depending on how your form is built, there is different ways to set the name of your form.
If you are creating the form through $this->createForm(CustomType::class):
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamed('custom_form_name', CustomType::class);
If you are building the form from the controller directly through $this->createFormBuilder():
$formFactory = $this->get('form.factory');
$form = $formFactory->createNamedBuilder('custom_form_name', CustomType::class);
Look at the FormFactory and FormBuilder APIs for more information.
You can try it, remove prefix on field name
public function getBlockPrefix()
{
return null;
}

Symfony: Two actions one route

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()
{
}
}

Doctrine entity callback not called

I have added #ORM\Entity and #ORM\HasLifecycleCallbacks annotations in my entity class and later in the entity class I have added the callback method with the right annotation (as following):
/**
*
* #ORM\PreUpdate
* #ORM\PrePersist
*/
protected function PreUpdateHandler()
{
echo '*********** PRE UPDATE *************';
var_dump('*********** PRE UPDATE *************');
return $this;
}
But the PreUpdateHandler is not getting called upon any DB manipulation (insert, update or delete). Any idea what am I missing?
BTW: Where can I see the list of all available event annotations (likes of #ORM\PreUpdate and #ORM\PrePersist)?
IMPORTANT!!!
My entity class is inherited from a base Entity class which is located in another directory (and namespace). I have added the HasLifecycleCallbacks annotation in the metadata of the base class as well. Does this matter for the callbacks to trigger?
Your lifecycle callback method PreUpdateHandler is not invoked because the visibility of method is protected and hence not accessible by the ORM. Change the visibility to public and try.
Try this and check your result
$em->persist($object);
$object->PreUpdateHandler()
$em->flush();
Replace your annotation callback
/**
*
* #ORM\PreUpdate
* #ORM\PrePersist
*/
To
/**
*
* #ORM\PreUpdate()
* #ORM\PrePersist()
*/
SO the thing is that I am using the yml file as well as the entity class and it seems as if the annotations doesn't work in parallel to the yml file. I removed the annotations and added the callbacks in the yml file and its working.

Categories