I am using Symfony 3.4.3.
This is my routing in
app/config/routing.yml:
blog:
resource: "#BlogBundle/Controller/"
type: annotation
this is my controller from BlogBundle/Controller/DefaultController.php
<?php
namespace BlogBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
/**
* #Route("/home", name="home_route")
*/
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('BlogBundle:Default:index.html.twig');
}
}
And when I go to http://localhost:8000/home address I am getting this error: No route found for "GET /home"
So, what am I doing wrong here?
You add to set the route annotation on the action
<?php
namespace BlogBundle\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="home_route")
*/
public function indexAction()
{
return $this->render('BlogBundle:Default:index.html.twig');
}
}
If you use annotation on the class, is to prefix all actions
Related
I was working on my first project with Symfony, I created my first controller. And this error appears.
problem image
I tried to
composer update
and I got the same problem!
The problem that appear when i update the composer
here is the code of TestController.php
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TestController extends Controller {
/**
* #Route("/")
* #Method ({"GET"})
*/
public function index()
{
//return new Response('<html><body>Hello world</body></html>');
return $this->render('articles/index.html.twig');
}
}
index.html.twig file has only a <h1> test </h1> on it
What makes that problem appear? and how to fix it without deleting the project and creating it again!
thanks
Symfony\Bundle\FrameworkBundle\Controller\Controller has been deprecated since v4.2.0 and removed since v5.0.0, use Symfony\Bundle\FrameworkBundle\Controller\AbstractController instead.
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TestController extends AbstractController {
/**
* #Route("/", methods={"GET"})
*/
public function index()
{
//return new Response('<html><body>Hello world</body></html>');
return $this->render('articles/index.html.twig');
}
}
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 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.
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
im new to symfony framework. it raises render error. i tried all suggestions
#App/default/index.html.twig
AppBundle:default:index.html.twig
default/index.html.twig
routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
lucky_number:
path: /lucky/number/{count}
defaults: { _controller: AppBundle:Lucky:number }
LuckyController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LuckyController
{
/**
* #Route("/lucky/number/{count}")
*/
public function numberAction($count)
{
$number = rand(0, 100);
$html = $this->render('#App/default/index.html.twig','number'=>$number);
//$html = $this->container->get('templating')->render('AppBundle:default:index.html.twig',array('number' => $number));
//$html = $this->container->get('templating')->render('default/index.html.twig',array('number' => $number));
return new Response($html);
}
}
Error:
Attempted to call an undefined method named "render" of class
"AppBundle\Controller\LuckyController".
You forgot to extend your controller class with the symfony controller.
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class LuckyController extends Controller // <-- HERE
{
/**
* #Route("/lucky/number/{count}")
*/
public function numberAction($count)
{
$number = rand(0, 100);
return $this->render('AppBundle:default:index.html.twig',array('number' => $number));
}
}