Hi Have followed the instructions to enable the SensioFrameworkExtraBundle here: http://symfony.com/doc/2.1/bundles/SensioFrameworkExtraBundle/index.html
Thereafter I create the below controller:
namespace Acme\DemoBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class MyController
{
/**
* #Template
*/
public function indexAction()
{
}
}
If set up a route pointing to the indexAction on this controller and browse to it, I get the following error:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
It looks like the SensioFrameworkExtraBundle is not actually enabled, but I cannot figure out why. I'm looking for advice.
The #Template annotation is working. As the error suggest, you must return something. If return array, it will be sent to template engine. Make sure the template exists.
public function indexAction()
{
...
$somedata = 'fill data';
return array('somedata' => $somedata);
}
Related
I'm new to Symfony 4, I'm using profiler to debug in a dev environment.
I'm trying to debug a function in controller/ But I can't use a logger or a dump nothing appear.
<?php
namespace App\Controller;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Psr\Log\LoggerInterface;
class CRUDController extends Controller
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function batchActionDownload(ProxyQueryInterface $selectedModelQuery)
{
$this->logger->debug('exampleMethod :: $ourVar at interesting time', [
'our_var' => $selectedModelQuery
]);
var_dump($selectedModelQuery);
dump($selectedModelQuery);
return new RedirectResponse(
$this->admin->generateUrl('list', $this->admin->getFilterParameters())
);
}
I have no error, but simply my debug isn't showing up.
Any Idea of what I'm doing bad ?
Normally the best way to define the route is to use the annotation. You are new and i don't how know you have defined your route.
But if seems that your functions isn't called. So try to set a route and call it in your browser.
/**
* #Route("/mycrud", name="mycrud")
*/
public function batchActionDownload(ProxyQueryInterface $selectedModelQuery)
Then you should be able to open your function in the browser under http://yourdomain/mycrud. Then you should see the debug bar and your var_dump.
The next thing is you redirect in your function. So you output something and redirect. You can't see the output in that place. You have to stop the code before output with an exit for example.
I'm facing a problem that I don't understand. I simply create a Controller with make:controller and everything works, but when I want to create a new method, my route annotation doesn't work (whereas the default one works normally). T
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* #Route("/default", name="default")
*/
public function index()
{
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
/**
* #Route("/", name="home")
*/
public function home() {
return $this->render('default/home.html.twig');
}
}
The / route redirects me to the default page of Symfony and any other name for the route returns the No route found error.
Also, PHPStorm tells me that Import' Symfony\Component\Routing\Annotation\Route is never used when you can see that they are there...
How to solve this ? Thanks !
Check your config/routing.yaml
I'm trying to write an API in Symfony 4. I've hit a problem with my controller methods when trying to use DependencyInjection for a service API class I created. I've tried several different ways to write the code and can not figure it out.
https://symfony.com/doc/current/components/dependency_injection.html
I can make a getNext() (instead of get() below) method and the code will function as expected, but if I try to use a get() method I will get an error. These are the basic classes involved. Most of the code has been removed.
class AppointmentController extends AbstractController
{
/**
* #Route("/appointment/getNext", name="appointment/getNext")
*
*/
public function get(string $id = null, CernerFhir $fhirApi)
{
$request = Request::createFromGlobals();
...more code...
}
}
class CernerFhir
{
public function __construct(LoggerInterface $logger, ParameterBagInterface $params)
{
$this->logger = $logger;
$this->params = $params;
}
}
}
Warning: Declaration of App\Controller\AppointmentController::get(?string $id, App\Service\CernerFhir $fhirApi) should be compatible with Symfony\Bundle\FrameworkBundle\Controller\AbstractController::get(string $id)
AbstractController uses an interface that defines a get() method with a specific number of parameter and return type. If you wan't to overwrite it's get method (which i do no recommend), you have to write it so that it's compatible with it's definition in the interface.
http://php.net/manual/en/language.oop5.interfaces.php
So, I've made an app with its own controller and entities. I've just installed the FOSUserBundle following the guide and I want to connect my app to the login, register, etc. I tried using the function 'path'. This is what looks like... 'href="{{path(menu.url)}}' ok, so I get an error: 'such route does not exist'. I decided to create another controller, this one called FosController, next to my previous one the DefaultController, I extended the FosController from SecurityController(login fosuserbundle) and decided to create with annotation a route called login and connect it to the parent method...
class FosController extends SecurityController
{
/**
* #Route("/hospitallogin", name="login")
* #Method({"GET", "POST"})
*
* #param Request $request
*
* #return Response
*/
public function login2Action(Request $request) {
parent::loginAction($request);
}
}
And I get this error:
The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?
I dont know what else to do, thanks for your time.
I decided to use this though it's not correct...
public function indexAction()
{
return $this->redirect('http://localhost:8000/login');
}
Does anyone know a way to get the template (or its name) that called the Twig extension?
You could pass the referrer into the twig template...
class DefaultController extends Controller
{
public function indexAction(Request $request)
{
return $this->render('DefaultBundle:Default:index.html.twig', array('referrer', $request->getRequestUri()));
}
}