I am trying to start on Symfony2 but ran into a problem right away following the Symfony 2 "the book" part "Creating pages in Symfony 2":
I did this:
Created the bundle
php app/console init:bundle "Acme\StudyBundle" src
*Added the namespace in app/autoload.php *
$loader->registerNamespaces(array(
'Acme' => __DIR__.'/../src',
));
Initialized the bundle
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Acme\StudyBundle\AcmeStudyBundle(),
);
// ...
return $bundles;
}
Created the routes in app/config.routing.yml and src/Acme/StudyBundle/Resources/config/routing.yml
# app/config/routing.yml
homepage:
pattern: /
defaults: { _controller: FrameworkBundle:Default:index }
hello:
resource: "#AcmeStudyBundle/Resources/config/routing.yml"
# src/Acme/StudyBundle/Resources/config/routing.yml
hello:
pattern: /hello/{name}
defaults: { _controller: AcmeStudyBundle:Hello:index }
Created the controller
// src/Acme/StudyBundle/Controller/HelloController.php
namespace Acme\StudyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
When I load the page: http://localhost/app_dev.php/hello/Ryan Symfony gives me an exception:
Unable to find controller "AcmeStudyBundle:Hello" - class "Acme\StudyBundle\Controller\HelloController" does not exist.
I got over the code several times but cannot find anything wrong.
just add
<?php
in the beginning of your controller file : src/Acme/StudyBundle/Controller/HelloController.php
it's solved the problem to me.
Afaik there is a discussion going on within the Symfony 2.0 dev guys in what places they should keep the "Bundles" extension.
I've just grabbed the latest version of Symfony via Git and followed your code 1:1.
I got various error messages too but when I changed...
in src/Acme/StudyBundle/Resources/config/routing.yml
defaults: { _controller: AcmeStudyBundle:Hello:index }
to
defaults: { _controller: AcmeStudy:Hello:index }
app/config/routing.xml
resource: "#AcmeStudyBundle/Resources/config/routing.yml"
to
resource: "#AcmeStudy/Resources/config/routing.yml"
...i got a pretty "Hello Ryan" in the browser.
Hope this helps!
You are probably running PR9. Update to PR11(latest), and I would bet this gets resolved. Symfony devs removed the 'Bundle' suffix in PR9, but added it back again shortly there after.
Also, Symfony devs keep an Update log that I find extremely helpful.
Related
I have a Symfony (4.1) project. In this project i need to create new route, like this.
Controller
/**
*
* #Route("/search-user", name="search-user")
*/
public function searchUser(Request $request)
{
dd("Search user");
return $this->render("XXX");
}
Routes.yaml
controllers:
resource: '../src/Controller/'
type: annotation
prefix: '/'
The problem: When i go to endpoint/search-user returns 404 not found.
I´m some new in Symfony, so somebody can see what's wrong?
You can launch
php bin/console debug:router
to have a list of all your existing routing.
Can you add your controller declaration?
check that is in the src/Controller folder
check that there is no routing prefix in your controller (#route before class declaration)
I have created a controller class under a module directory in Symfony 4. My bundle's routing file references it. My root's routing file includes the bundle's routing file.
However I get an error 500, "Error: the controller does neither exist as service nor as class".
Why?
root/src/MyBundle/Resources/config/routing.YML
route_name:
path: /test3
controller: MyBundle\Controller\ExportCsvController::exportProductInCsv
options:
expose: true
root/config/routes/my_custom_routes.YML
route_name:
resource: "#MyBundle/Resources/config/routing.yml"
prefix: /
root/src/MyBundle/Controller/ExportCsvController.PHP
<?php
namespace MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ExportCsvController extends Controller
{
public function exportProductInCsv(): Response
{
return new Response(
'<html><body>test</body></html>'
);
}
}
It was a problem of cache (Symfony cache). Solved with this command: php bin/console cache:clear --env=prod. Now the content of the file root/src/MyBundle/Resources/config/routing.YML is correctly taken into account :).
I'm using Symfony2 and i'm trying to POST a form to my controller, but my controller take it as a GET request everytime... Even when i'm using restclient, i put POST for the request but when i use $request->getMethod(), the answer is always GET :/
The source code is really simple :
<?php
namespace TC\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class UserController extends Controller
{
/**
* #Route("/user/register")
* #Method({"POST"})
*/
public function registerAction(Request $request)
{
echo $request->getMethod();
return new Response();
}
}
And when i add the requirements _method: POST like this :
tc_user_register:
path: /user/register/
defaults: { _controller: TCUserBundle:User:register }
requirements:
_method: POST
I got this :
No route found for "POST /user/register"
Whatever i do, Symfony2 take it as a GET request, any idea ?
Thanks !
Couple of things #Xtroyer:
1 If you use routing.yml file, requirements: _method is not recognised since Symfony 2.2, it is now:
tc_user_register:
path: /user/register/
defaults: { _controller: TCUserBundle:User:register }
methods: [POST]
2 If you use Annotations. #Method is not recognised if you don't mention the following use statement in your controller:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
3 You can check what route is exactly registered with the following command?
php app/console router:debug | grep tc_user_register
You should have:
Name Method Scheme Host Path
tc_user_register POST ANY ANY /user/register/
I look for the way to inject more parameters into symfony configuration cache. Currently, I use kernel.cache_warmer hook to my class in services.yml to generate another yml file in a directory. Then, it will be include in the symfony configuration cache, are there any possible way to inject a variable into generated config cache without need to create the Yml file?
Basically, I would like to make cache key changed everytime when run app/console cache:clear. Here is my service,
services.yml
imports:
- { resource: version.yml }
services:
cacheManager:
class: "%cacheManager.class%"
calls:
- [ setCachePrefix, ["%memcache.deploymentPrefix%"]]
memcacheDataVersioning:
class: WarmUpListener
tags:
- { name: kernel.cache_warmer, priority: 0}
WarmUpListener.php
class WarmUpListener implements CacheWarmerInterface{
public function warmUp($dir)
{
$array = ['parameters' => ['memcache.deploymentPrefix' => date('Ymd')]];
$dumper = new Dumper();
$yaml = $dumper->dump($array);
file_put_contents(__DIR__ . '/../Resources/config/version.yml', $yaml);
}
public function isOptional()
{
return false;
}
}
I have added to the DependencyInjection/*Extension class as below
DependencyInjection/somethingExtension.php
$container->setParameter('memcache.deploymentPrefix', date('Ymd') );
This will help to inject the variable in the configuration cached without need to make the Yml file and can removed all warmUp hookup on the question.
When dealing with the Sylius e-commerce bundles I have found what seems to be a way of configurig the template for a route, that I didn't know:
I have tested in a fresh Symfony RC 2.2.0 with vendors installation.
This would be in the routing.yml
_welcome:
pattern: /
defaults:
_controller: AcmeDemoBundle:Welcome:index
_template: AcmeDemoBundle:Welcome:index # added by me
this generates an error:
FatalErrorException: Error: Call to a member function getTemplate() on
a non-object in
.... \vendor\sensio\framework-extra-bundle\Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener.php
line 62
now, in TemplateListener, what we have is:
if (!$configuration = $request->attributes->get('_template')) {
return;
}
if (!$configuration->getTemplate()) {
$guesser = $this->container->get('sensio_framework_extra.view.guesser');
$configuration->setTemplate($guesser->guessTemplateName($controller, $request, $configuration->getEngine()));
}
$configuration is a String, actually the template I put in routing.yml (AcmeDemoBundle:Welcome:index). Checked by adding a var_dump and also inspecting ParameterBag -> get method which is what $request->attributes is.
So. Why TemplateListener is expecting an object? What am I missing? Am I missconfiguring in routing.yml?
This parameter is not available in Symfony itself.
Feature is provided by SyliusResourceBundle and available only in Sylius controllers.
And apparently _template request attribute conflicts with SensioFrameworkExtraBundle, which uses same name to store object.
We have to move those parameters one config node deeper, to avoid such problems in future.
You can keep an eye on the https://github.com/Sylius/SyliusResourceBundle repository, fix should arrive today.