Symfony: Getting basic routing to work - symfony

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.

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 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!

symfony2 unable to find twig template

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.

Overriding the FOSUserBundle controllers

So I read alot about the overriding of templates and such and overriding of bundles in Symfony.
I am using the new Symfony 2.3, I have not tried this in lower versions of Symfony.
I followed the tutorial about overriding bundles in Symfony:
http://symfony.com/doc/2.3/cookbook/bundles/inheritance.html
I followed the tutorial about overriding the controllers of FOSUserBundle, which is the same thing really:
https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_controllers.md
I had a bundle named Acme/WebBundle.
Now I have done the following things:
Created a new bundle named Acme/UserBundle.
Created the file AcmeUserBundle.php in this bundle.
<?php
namespace Acme\UserBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
public function getParent()
{
return 'FOSUserBundle';
}
}
Created the following file structure:
-src
-Acme
-UserBundle
-Controller
RegistrationController.php
-Entity
User.php
-Resources
-translations
-views
AcmeUserBundle.php
In RegistrationController.php I set the namespace to:
namespace Acme\UserBundle\Controller;
Copied the contents of the registration controller of FOSUserBundle to mine.
Added to the beginning of registerAction()
die("message");
Now when I go to the registration form, the default /register route, I don't get a die, everything works fine. It does not see my bundle as a child, nothing is overridden and I've been trying to get it to work for ages hence my question here.
Did I do something wrong?
Remember that you need to add any new bundle to AppKernel::registerBundles() in app/AppKernel.php like this:
$bundles = array(
...
new Acme\UserBundle()
);

Resources