I have written a series of working controllers within the directory /src/AppBundle/Controller/Admin, such as DoctorAdminController, PatientAdminController, etc. The top of these controllers look like so:
<?php
namespace AppBundle\Controller\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* #Route("/admin")
*/
class DoctorAdminController extends Controller
{
/**
* #Route("/doctor", name="admin_doctor_list")
*/
public function indexAction()
{
// stuff
}
}
I can navigate to each page exactly as expected:
http://example.com/admin/doctor
I would like to have a page sitting at http://example.com/admin, but for the life of me can't get get a working route. I have this as the controller for the index page:
<?php
namespace AppBundle\Controller\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* #Route("/admin")
*/
class DefaultAdminController extends Controller
{
/*
* #Route("/", name="default_admin")
*/
public function indexAction()
{
$this->render('admin/admin_base.html.twig');
}
}
I have my routing.yml file set to annotations:
app:
resource: "#AppBundle/Controller/"
type: annotation
When I try to access http://example.com/admin, I get a "No route found for GET /admin" error. Consistent with this, when I use ./bin/console debug:router there is no route listed for /admin, but I do see routes for all others (/admin/doctor, etc.)
What am I missing?
This is because you have your routing annotation before the class. You shouldn't do that, but instead put it in front of your actions only, like so:
<?php
namespace AppBundle\Controller\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultAdminController extends Controller
{
/*
* #Route("/admin", name="admin")
*/
public function indexAction()
{
$this->render('admin/admin_base.html.twig');
}
}
I also show this with the "name" using "admin" as well, so you can also specify "admin" in Twig and redirects using the same name; this is so it will make sense to you. Same thing for your DoctorAdminController, remove the routing annotation before the class.
That should fix the problems.
Related
I'm trying to render a theme index file that I'm trying to import to pimcore to work with. I've put the theme folder in app/Resources/views.
I've added the following to my routing.yml file
index:
path: /static/dist/index.html
controller: AppBundle\Controller\indexController::index
below is my controller named indexController.php
<?php
namespace Appbundle\Controller;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class indexController extends FrontendController
{
/**
* #Route("/static/dist/index.html", name="homepage")
*/
public function index()
{
return $this->render('static/dist/index.html');
}
}
When I try to navigate to localhost/static/dist/index.html, I get the following error:
No engine is able to work with the template "static/dist/index.html".
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
First of all, I have to say that I have been seeing answers and documentation for several days but none of them answer my question.
The only and simple thing I want to do is to use the twig service as a global service in a BaseController.
This is my code:
<?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Service\Configuration;
use App\Utils\Util;
abstract class BaseController extends Controller
{
protected $twig;
protected $configuration;
public function __construct(\Twig_Environment $twig,Configuration $configuration)
{
$this->twig = $twig;
$this->configuration = $configuration;
}
}
Then in all my controllers extend the twig and configuration service, without having to inject it again & again.
//...
//......
/**
* #Route("/configuration", name="configuration_")
*/
class ConfigurationController extends BaseController
{
public function __construct()
{
//parent::__construct();
$this->twig->addGlobal('menuActual', "config");
}
As you can see the only thing I want is to have some services global to have everything more organized and also to create some global shortcuts for all my controllers. In this example I am assigning a global variable to make a link active in the menu of my template and in each controller I have to add a new value for menuActual, for example in the UserController the variable would be addGlobal('menuActual', "users").
I think this should be in the good practices of symfony which I don't find :(.
Having to include the \Twig_Environment in each controller to assign a variable to the view seems very repetitive to me. This should come by default in the controller.
Thanks
I've had that problem as well - trying to not have to repeat a bit of code for every controller / action.
I solved it using an event listener:
# services.yaml
app.event_listener.controller_action_listener:
class: App\EventListener\ControllerActionListener
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
#src/EventListener/ControllerActionListener.php
namespace App\EventListener;
use App\Controller\BaseController;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
/**
* Class ControllerActionListener
*
* #package App\EventListener
*/
class ControllerActionListener
{
public function onKernelController(FilterControllerEvent $event)
{
//fetch the controller class if available
$controllerClass = null;
if (!empty($event->getController())) {
$controllerClass = $event->getController()[0];
}
//make sure your global instantiation only fires if the controller extends your base controller
if ($controllerClass instanceof BaseController) {
$controllerClass->getTwig()->addGlobal('menuActual', "config");
}
}
}
Impossible to load Stripe classe in my Symfony controller see :
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Stripe\Stripe;
class PointsController extends Controller
{
/**
*
* #Route("/points/buy", name="points_buy")
*/
public function buyAction(Request $request)
{
\Stripe\Stripe::setApiKey('sk_test_');
return $this->render('points/buy.html.twig', [
]);
}
}
Stripe added with composer in vendor directory
I tried Stripe::setApiKey('sk_test_') but same error ...
Any idea?
If you're importing Stripe\Stripe in your use statement then I don't think you need the fully qualified class name.
use Stripe\Stripe;
//// etc.
/**
*
* #Route("/points/buy", name="points_buy")
*/
public function buyAction(Request $request)
{
Stripe::setApiKey($secretKey);
}
Here's a Stripe service I created for one of my projects.
I have uploaded to my godaddy webhost this cotroller in my symfony
<?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\Session\Session;
class DefaultController extends Controller {
/**
* #Route("/contact")
*/
public function contactAction(Request $request) {
/* my contact action */
}
/**
* #Route("/", name="homepage")
*/
public function indexAction(Request $request) {
/* my index action */
}
}
the problem is with my contact action. when I go to www.myserver.net/contact it returns 404 response, but it loads normally the index action. What do I do wrong?
Make sure your app/config/routing.yml contains this:
app:
resource: "#AppBundle/Controller/"
type: annotation