symfony2 unable to find twig template - symfony

I am 2 hours new into Symfony2 so excuse my noob question. I am receiving an error stating:
Unable to find template "Akademiah:MemberBundle:Default:index.html.twig".
500 Internal Server Error - InvalidArgumentException
3 linked Exceptions: Twig_Error_Loader » InvalidArgumentException » InvalidArgumentException »
Not sure why this is showing as I am following a tutorial and things are expected to work at this stage?
I have the following in my controller:
<?php
namespace Akademiah\MemberBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* #Route("/hello/{name}")
* #Template()
*/
public function indexAction($name)
{
return $this->render('MemberBundle:Default:index.html.twig',array('name'=>$name));
}
}
and I created the bundle using the PHP app/console command. Help is appreciated.

You should check if a file called index.html.twig exists in your resources/twig/default/ folder. If it does not, create it.

Related

Can't use Route

Route("/) does not work.
I already tried to reinstall annotations and Symfony with Composer.
My routes.yaml :
index:
path: /
controller: App\Controller\ArticleController::homepage
My ArticleController.php :
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ArticleController
{
/**
* #Route("/")
*/
public function homepage()
{
return new Response("TEST");
}
/**
* #Route("/news/test-test")
*/
public function show()
{
return new Response("FUTURE");
}
}
The page always shows the public/index.php, but it should show "FUTURE" ;)
in your config folder you should find a folder called routes inside it should be a file called annotations.yaml which contains the following :
controllers:
resource: ../../src/Controller/
type: annotation
if you have this you should get "TEST" when you go to ("/")
and you get "FUTURE" when you go to (/news/test-test)
and make sure to clear the cache if it did not
and you have to comment every thing inside routes.yml
Like Ali Mhanna said, check first annotations.yaml.
Then, according to Symfony documentation (read it, it can help ;), run php bin/console debug:router
If you see your route, your route is working.

symfony 3.2.4 ... Unable to find template "OCPlatformBundle:Advert:index.html.twig

I am using symfony 3.2.4 (got with console --version).
I am working the openclassroom tutorial.
That controller always return the error below.
Nevertheless, the file below exist with and is accessible.
src/OC/PlatformBundle/Resources/views/Advert/index.html.twig
I got crazy with that. Does someone have an idea ?
Thanks in advance.
<?php
// src/OC/PlatformBundle/Controller/AdvertController.php
namespace OC\PlatformBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class AdvertController extends Controller
{
public function indexAction()
{
$content = $this->get('templating')->render('OCPlatformBundle:Advert:index.html.twig');
return new Response($content);
}
}
Unable to find template "OCPlatformBundle:Advert:index.html.twig"
(looked into: /var/www/html/symfony/app/Resources/views,
/var/www/html/symfony/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form).
*
Did you try to create your template in app/Resources/views/Advert/index.html.twig ?
This is a better practice : Store all your application's templates in app/Resources/views/ directory. (From SF doc)...
So, you'll be able to "call" it by this way:
/***/
public function indexAction()
{
return $this->render('Advert/index.html.twig', []);
}
/***/

Symfony: Getting basic routing to work

this maybe due to my stupidity, but I could really use a hint here.
I have not done much with php so far, been mostly working with javascript, and now trying to get a grasp on Symfony. I'm working with vagrant and the great scotch-box, so I installed Symfony on this vm and created a new app. I tried creating a route that just returns "Hello world!", but i can't get it to work.
The route defined in the DefaultController is working. I created a new controller via the console command
$ php bin/console generate:controller
and called it TestController. This is it:
<?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;
class TestController extends Controller
{
/**
* #Route("/test")
*/
public function testAction()
{
return new Response( "Hello World!" );
}
}
When navigating to [scotch-box address]/my_project/web I get the default route, but [scotch-box address]/my_project/web/test gives me a 404.
Maybe it has to do with the configuration of the vm, but this is not my expertise, so any nudge in the right direction will be appreciated. Thank you!
Edit: After I managed to destroy my vm yesterday, I set up a clean scotch-box and installed symfony's demo-app, which is running just fine. I also have setup a clean Symfony project and changed only the DefaultController's route to "/home":
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/home", name="homepage")
*/
public function indexAction(Request $request)
{
// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
]);
}
}
my routing.yml reads:
app:
resource: "#AppBundle/Controller/"
type: annotation
As I understand it, Symfony's welcome page should now be reachable via [scotch-box address]/my_project/web/home, but it isn't. Instead I can still reach it via [scotch-box address]/my_project/web, while /home throws a 404 page.
Maybe I'm wearing the idiot hat here, but I could not find anything in the documentation that would shed a light on this.
I may be wrong, but I'm pretty sure that it's because your routing.yml file is incomplete. Check if you have this in routing.yml:
app:
resource: "#AppBundle/Controller/"
type: annotation
If not, add it. This will "plug in" the annotation routing of your AppBundle into the routing of the whole project.

Why symfony can't find template it rendered in other function

I have a function in my controller like this:
<?php
namespace GradeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use GradeBundle\Entity\User;
use GradeBundle\Entity\SchoolClass;
class MainController extends Controller
{
/**
* #Route("/", name="index_page")
*/
public function index()
{
return $this->render('GradeBundle:Default:index.html.twig');
}
It renders the twig template correctly. However when I use other function:
/**
* #Route("/adminNew", name="add_admin")
*/
public function addAdmin()
{
$session = new Session();
if($session->get('loggedIn') == null)
return $this->redirect($this->generateUrl('index_page'));
else
return $this->render('GradeBundle:Add:newAdmin.html.twig');
}
I have the following error:
Unable to find template "GradeBundle:Default:index.twig.html".
Does anybody have any idea what might be wrong?
It's a typo somewhere you call template:
GradeBundle:Default:index.twig.html
But you have only GradeBundle:Default:index.html.twig template.
Note the difference: html.twig twig.html
I suspect that you extend it in GradeBundle:Add:newAdmin.html.twig by:
{% extends 'GradeBundle:Default:index.twig.html' %}
but should be:
{% extends 'GradeBundle:Default:index.html.twig' %}
Have you made sure to use the correct namespace for the Controller you're using? And are you including the correct files? Also I'm not sure I understand the question correctly - are you saying if you add another function with a different twig file render, the first one no longer renders? Could I see your class names and the namespaces / use statements?
Usually in these instances, it's that the templates are in the wrong place or the correct file is not included in order to find it.
Michael

Symfony 2/Sylius - Bundle loaded in AppKernel but can't load resource

I've generated a bundle (#ShopfishApiBundle) using generate:bundle like I have many times. It has automatically registered the bundle in theAppKernel and it added the loading of the bundle's routing.yml to the app/Resource/config/routing.yml as well. This is within a Sylius installation running Symfony 2.3
The #ShopfishApiBundle/Resource/config/routing.yml looks like this:
shopfish_api:
resource: "#ShopfishApiBundle/Controller/ProductController.php"
type: annotation
The product controller looks like this:
namespace Shopfish\Bundle\ApiBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* #Route("/api")
*/
class ProductController extends Controller
{
/**
* #Route("/products")
* #Method({"GET"})
*
* #Rest\View
*/
public function allAction()
{
$products = array();
return array('products' => $products);
}
}
Loading any page instantly yields the below exception:
FileLoaderLoadException: Cannot load resource "#ShopfishApiBundle/Controller/". Make sure the "ShopfishApiBundle" bundle is correctly registered and loaded in the application kernel class.
In another Symfony2 (version 2.4) application I've made a similar bundle and this worked without error, I'm thinking something in Sylius messes this up. Do you know where I might solve this issue?
NOTE: I did a little test to see if a direct-no-annotations code snippet worked, and that seems to work. Though I want to use the FOS Rest bundle, using Annotations for Routing.
sf_api_controller:
pattern: /no-annotation-test
defaults:
_controller: ShopfishApiBundle:Product:all
I hadn't registered the essential SensionFrameworkExtraBundle in my AppKernel.php:
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle()
Thanks, #pazi!

Resources