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
Related
Hi I'm fresher for symfony . when i execute the below code it will show error.i installed all the assets of symfony.help me to clear this..!
<?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 StudentController extends Controller
{
/**
* #Route("/")
* #Method({"GET"})
*/
public function index()
{
// return new Response(
// '<html><body><h1>HI,Welcome to Tamilnadu</h1></body></html>'
// );
return $this->render('student/index.html.twig');
}
}
I just put "hello" in (student/index.html.twig),and i commented the basic response function and want to run the return this function.
Try this:
/**
* #Route("/", name="index")
* #return Response
*/
public function index():Response{
return $this->render('student/index.html.twig');
}
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
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 want to implement service controller as rest controller which not extends from fosRestController, this mean i inject the service by my self. But i get always the error that:
Can't locate "api.contact_controller:postContactAction" controller in api.contact_controller:postContactAction (which is being imported from "/var/www/XXXX/XXXXX/XXXXX/app/config/routing.yml").
Service class:
api.contact_controller:
class: XXXX\ApiBundle\Controller\ContactController
parent: XXXX_rest_controller
arguments: [ "#templating"]
Then the routing:
contact:
type: rest
resource: 'api.contact_controller:postContactAction'
Finally the Controller
namespace XXXXX\ApiBundle\Controller;
use FOS\RestBundle\Routing\ClassResourceInterface;
use XXXXX\ContentBundle\Form\ContactFormType;
use XXXXX\Frontend\Controller\RestController;
use XXXX\Frontend\Model\Contact;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ContactController
*
* #package XXXXX\ApiBundle\Controller
*/
class ContactController extends RestController
{
/**
* #var EngineInterface $templating
*/
private $templating;
/**
* ContactController constructor.
* #param EngineInterface $templating
*/
public function __construct (EngineInterface $templating)
{
}
public function postContactAction (Request $request)
{
echo '<pre>';
print_r($request);die;
}
}
The Form:
<form id="contactform" method="post" action="{{ path('post_contact') }}" name="contactform">
.....
</form>
Now it works like below:
Routing definition
contact:
type: rest
resource: "api.contact_controller"
Without action in it!
/src/Vendor/JobQueueBundle/Controller/DefaultController.php
namespace Vendor\JobQueueBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\JobQueueBundle\Controller\JobController;
/**
* #Route("/jobs")
*/
class DefaultController extends JobController
{
/**
* #Route("/index")
*/
public function indexAction()
{
die();
}
}
/app/config/routing.yml
vendor_api_job_queue:
resource: "#VendorJobQueueBundle/Controller/"
type: annotation
prefix: /
JMSJobQueueBundle:
resource: "#JMSJobQueueBundle/Controller/"
type: annotation
/src/Syntetik/API/JobQueueBundle/SyntetikAPIJobQueueBundle.php
namespace Vendor\JobQueueBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class VendorJobQueueBundle extends Bundle
{
public function getParent()
{
return 'JMSJobQueueBundle';
}
}
When I try to call /jobs/index I get:
Method "JMS\JobQueueBundle\Controller\JobController::indexAction" does not exist.
DefaultController is completely ignored and not sure why?
Thanks!
It's known and opened issue of JMSDiExtraBundle https://github.com/schmittjoh/JMSDiExtraBundle/issues/39 so problem is that DiExtarBundle doesn't lookup parent class for annotations if child class has not at least one JMS annotation, so proxy class isn't generate at the metadata cache (look app/cache/dev/jms_diextra/metadata/)
The quickest solution is leave at least on annotation:
Parent Controller >>
<?php
namespace Namespace\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\DiExtraBundle\Annotation as DI;
class IndexController extends Controller
{
/**
* #DI\Inject("doctrine.orm.entity_manager")
* #var \Doctrine\ORM\EntityManager $em
*/
protected $em;
/**
* #DI\Inject("namespace.search.manager")
* #var Namespace\SearchBundle\Services\SearchManager $searchManager
*/
protected $searchManager;
/**
* #DI\Inject("namespace.product.manager")
* #var Namespace\ProductBundle\Services\ProductManager $productManager
*/
protected $productManager;
/**
* #Route("/", name="homepage")
* #Template()
*/
public function indexAction() {
echo "parent!";
$defaultCategory = $this->searchManager->getDefaultCategory();
....
return $result;
}
}
Child controller >>
<?php
namespace OtherSpace\SiteBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\DiExtraBundle\Annotation as DI;
use Namespace\SiteBundle\Controller\IndexController as BaseIndexController;
class IndexController extends BaseIndexController
{
/**
* The temprary solution base on known JMS/DiExtraBundle open issue https://github.com/schmittjoh/JMSDiExtraBundle/issues/39
* **We need to leave at leat one Inject in child class to get a proxy generated**
*
* #DI\Inject("doctrine.orm.entity_manager")
* #var \Doctrine\ORM\EntityManager $em
*/
protected $em;
/**
* #Route("/", name="homepage")
* #Template()
*/
public function indexAction() {
echo "child!";
$result = parent::indexAction();
return $result;
}
}
So this way proxy class will be generated for SpaceOther-IndexController and annotations will work
I also worked around fix this issue, you can look to my pull request https://github.com/schmittjoh/JMSDiExtraBundle/pull/153
Figured out the problem with this. It seems it's not symfony2 specific.
The problem is JMS\DiExtraBundle\JMSDiExtraBundle which mess the things around. Just removing that bundle makes everything works by the book.
Thanks!