I have a route like this:
/**
* #Route("/admin/user/edit/{id}", defaults={"id" = 0}, name="admin_user_edit")
* #Route("/admin/user/edit")
*
*/
public function editAction(Request $request, $id = 0){
(...)
And then a Twig template, where I list all users:
{% for user in users %}
<li>
<a href='{{ path('admin_user_edit'),{'id' : user.getId()} }}'>
{{ user.getUsername() }}
</a>
</li>
{% endfor %}
As you can see I'm trying to pass the 'user.getId()' as an id. And I get this:
Unexpected token "punctuation" of value "," ("end of print statement"
expected) in admin/users/list.html.twig at line 5.
I know it's about this user.getId() because everything was fine before I added this piece of code.
So how can I pass a function result to this "path" function?
Your path function should have the route name and the parameters within the parenthesis like
{{ path('admin_user_edit', {'id' : user.getId() }) }}
Also, as you aren't using arguments in your user.getId() call you can just use user.id instead.
Related
is there a way to get the current route {placeholder} value in symfony?
I have :
/**
* #Route("/ideas/{page}",
* defaults={"page":1},
* requirements={"page":"^\d+$"},
* name="ideas")
*/
public function ideasAction($page) {
.....
return $this->render('idea/idea.html.twig', ["ideas" => $ideas]);
}
And in my twig I want to do something like this :
>
Is it possible?
Like Gara said :
With {{ app.request.get('page') + 1 }} you can get your placeholder
So this works :
{% set page = app.request.get('page') + 1 %}
>
After trying to create a delete method, I am experiencing the following error:
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("id") to generate a URL for route "movie_delete".") in movies/index.html.twig at line 10.
The code I am using:
Twig template:
{% extends 'base.html.twig' %}
{% block body %}
{% if movies|length == 0 %}
There are no movie items available. Add a movie here to get started.
{% elseif movies|length != 0 %}
These are the results: <br />
<ul>
{% for x in movies %}
<li>Title: {{ x.title }} - Price: {{ x.price }} - Edit - Delete</li>
{% endfor %}
</ul>
Add more movie entries
{% endif %}
{% endblock %}
Delete class:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use AppBundle\Entity\Movie;
class MovieDeleteController extends Controller
{
public function deleteAction($id)
{
$em = $this->getDoctrine()->getManager();
if(!$id)
{
throw $this->createNotFoundException('No ID found');
}
$movie = $this->getDoctrine()->getEntityManager()->getRepository('AppBundle:Movie')->Find($id);
if($movie != null)
{
$em->remove($movie);
$em->flush();
}
return $this->redirectToRoute('movies');
}
}
And my routing.yml:
movie_delete:
path: /movies/delete/{id}
defaults: { _controller: AppBundle:MovieDelete:delete }
Can anyone explain me how I should add a Delete method in Symfony so that I can apply the change in the above written code?
You've forgot to pass the 'ID' variable to the twig route.
Delete
Should be
Delete
In your 'movie_delete' route, you've defined an 'id' parameter. This parameter is required for creating this route. Pass the missing parameter in your twig file and you're done!
In case you're going to work on your 'edit' route as well, keep in mind you will need an 'id' parameter as well. Make sure to pass it in your twig file as you did with the 'delete' route.
See: This Symfony documentation, Here the additional parameters for the twig routing are explained. In your case 'slug' is replaced by 'id'.
Good luck!
You didn't pass the id to the path() function. What you should've done is:
Delete
The path() function uses Symfony routing and will throw an exception if you forget to specify required attributes.
In your twig code, change the following line :
Delete
To :
Delete
Like this, the route will be generated using x.id as the id parameter.
Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}
Should it work in Silex with symfony/twig-bridge ?
{{ render(controller('MyController')) }}
Now I have message like this:
Twig_Error_Syntax: The function "controller" does not exist in "...
You can use it this way:
{{ render(path('your_route_id', {'id': id, 'anotherParam': param})) }}
I've found this working:
{{ render(controller('services.controller:action', {[params]}) }}
And you can define the controller as a service:
$app['services.controller'] = function() use ($dependecy1, .., $dependencyN){
return new \\PathToYourControllerClass($dependecy1, .., $dependencyN);
}
I've found this working :
{{ render(controller('Full\\Namespace\\To\\Your\\Controller::listAction')) }}
please don't forget a double slash '\\'
Example:
{{ render(controller('Acme\\ProductController::listAction')) }}
In Your ProductController (I'm using Doctrine 2 in this example) :
public function listAction(Application $application)
{
$em = $application['orm.em'];
$produits = $em->getRepository('Acme\Entity\Produit')->findAll();
return $application['twig']->render('list.html.twig', array(
'products' => $products
));
}
Then in your list.html.twig
{% for product in products %}
<h2> {{ product.name }} </h2>
{% endfor %}
How to call a variable's method in expression syntax in twig tempting system.
see the example below
{{ myObj.someMethod() }} {# this print the output of this method #}
i don't want above code because its printing output of this method.
but i want this
{% myObj.someMethod() %}
but its is giving me error that
Unknown tag name "myObj" in "
{% myObj.someMethod() %}" at line 2
even with above error method is also being called.
In twig syntax {{ some variable}} will print the result in variable you need to set variable and then use where you want
{% set myvar = myObj.someMethod() %} /* this will store the result returned from function */
{{ myvar }} /* this will print the result in myvar */