Symfony Route not found - php

I'm really new with symfony, and I have strange problem.
I have a default controller, which looks like:
Both Controllers located:
src/AppBundle/Controller/
Names:
DefaultController.php
and
CmsController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* #Route("/")
*/
public function indexAction(Request $request)
{
die('Homepage');
}
}
And I'm trying to create new one:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class CmsController extends Controller
{
/**
* #Route("/cms")
*/
public function cmsAction(Request $request)
{
die('Cms Page');
}
}
Routing file looks like:
app:
resource: "#AppBundle/Controller/"
type: annotation
When I try to go for www.domainname.com - default controller shows "Homepage" - as it should.
When I try to go for www.domainname.com/cms - it gives error 404.
What can be the problem?

Probelm was simply with cache. Clearing it solved the problem.

Related

Extending my own controller in Symfony

I am creating a webapp that has some common functions. So I figured the easiest way to do this would be to make a base controller and just extend that. So in the base controller I have (similar to):
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class BaseController extends Controller
{
protected function dosomething($data)
{
return $data;
}
}
And then in the default controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends BaseController
{
/**
* #Route("/", name="homepage")
*/
public function indexAction()
{
$data = "OK";
$thedata = $this->dosomething($data);
}
}
And then for the Admin Controller:
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class AdminController extends BaseController
{
/**
* #Route("/", name="homepage")
*/
public function indexAction()
{
$data = "OK";
$thedata = $this->dosomething($data);
}
}
However, I am getting errors like "Compile Error: Access level to AppBundle\Controller\AdminController::dosomething() must be protected (as in class AppBundle\Controller\BaseController) or weaker", not just when I load the admin controller function, but default as well. When I stop the admin controller extending base controller, this error goes (seems to work on default but not admin).
I'm guessing somewhere I have to let Symfony know that the admin controller is safe or something?
It has nothing to do with Symfony, it's PHP.
Obviously, you're trying to redefine dosomething method in your Admin Controller, and trying to make this method private.
It's not allowed. It may be either protected or public.
It's principle of OOP. Because if you would have a class SubAdminController, then instance of it would be also instance of both AdminController and BaseController. And PHP must definitely know if the method dosomething from parent class is accessible from SubAdminController.

No route found from another bundle

I have added a second bundle and when I try to open some url from that bundle, I keep getting an error that the route was not found. Adding the same route to the main bundle works perfectly.
What's wrong?
This is my project structure. I also:
- added the UserBundle to AppKernel.php (IDE shows the class exists)
- use AppBundle\Controller namespace in AppBundle, and UserBundle\Controller namespace in UserBundle
The controller I try to access from the UserBundle looks like this:
namespace UserBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
/**
* #Route("/login", name="user_login")
*/
public function loginAction(Request $request)
{
return array();
}
}
I suspect you need to add the UserBundle to your routing.yml configuration. You should have something like this in the routing.yml:
user_bundle:
resource: "#UserBundle/Controller/"
type: annotation

Symfony2 - Creating a URL

I've just started using Symfony. I want to echo the $bookid when I call a URL like book/5 , but I stuck somewhere.
Here's my DefaultController.php file
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;
class DefaultController extends Controller {
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction() {
return $this->render('default/index2.html.php');
}
}
file: /Myproject/app/Resources/views/default/index2.html.php
<?php
echo $id;
?>
When I call the book/6 , I get a blank page. What's missing? Do I need to change somewhere else, too?
You should declare that variable in your action and pass it to your view.
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;
class DefaultController extends Controller
{
/**
* #Route("/book/{id}", name="book")
*/
public function indexAction($id)
{
return $this->render('default/index2.html.php', array(
'id' => $id
));
}
}
Whenever you have a parameter defined in your URL, you also need to "declare" it in your action function, so symfony maps it. Then, if you want to use it in your view, you have to pass it along.
If you are just starting with Symfony, I strongly recommend reading the Symfony Book and Cookbook. They are full of examples and relatively easy to understand, even for a newbie.
Other than that, the answer from smottt is correct. You add {id} in your route definition and receive it as parameter in your controller action.
Symfony book: Routing

Symfony controller: redirect, route or something else?

So I'm building a Symfony web app.
I have a simple controller (DefaultController.php) as follows:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/**
* #Route("/", name="default")
*/
public function defaultAction(){
return $this->render('default/hello.html.twig', array(
'name' => "hello"
));
}
}
Nothing special.
Now, I would like to have a separate .php file called APIController.php that gets called when a user navigates to http://eamorr.com/api/
APIController.php would then handle requests such as:
http://eamorr.com/api/getUser
http://eamorr.com/api/addUser
http://eamorr.com/api/getAllUsers
...
Here's what APIController.php should look like:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class APIController extends Controller
{
/**
* #Route("/getUser", name="getUser")
*/
public function getUser(){
//
}
/**
* #Route("/addUser", name="addUser")
*/
public function addUser(){
//
}
/**
* #Route("/getAllUsers", name="getAllUsers")
*/
public function getAllUsers(){
//
}
//etc.
}
From an architectural point of view, am I doing this right? Does anyone have any recommendations as to how to do this in Symfony?
If you define routes like:
/**
* #Route("/getUser", name="getUser")
*/
public function getUser(){
//
}
Then the URL to this action will be http://eamorr.com/getUser. As you can see there's no /api part, and this is because you didn't mention it anywhere.
You have two solutions for this case.
First it to define full routes like
/**
* #Route("/api/getUser", name="getUser")
*/
public function getUser(){
//
}
Second: since you want all APIController actions to have this /api part, you can define a prefix for all routes by defining "base" route for whole class.
/**
* #Route("/api", name="getUser")
*/
class APIController extends Controller
Then you can leave your actions' routes like they are.
More info: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html#route-prefix
You are on the right track. But if you are looking to support multiple request and response formats (eg. JSON, XML) you are better off using FOSRestBundle.
It can handle content negotiation, entity serialization (using JMSSerializerBundle which is used by the FOSRest Bundle) and it let's you build RESTful routes.

Symfony2: No route found /demo/contactus

I am new to symfony and following the AcmeDemo i created a new page for Contact Us. At the moment it is working for the url localhost:8000/contactus but following the same implementation of demo/secured/login it should be localhost:8000/demo/contactus which gives 404.
I am not sure what wrong i am doing.
routing.yml
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
ContactusController
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class ContactusController extends Controller
{
/**
* #Route("/contactus", name="_demo_contactus")
* #Template()
**/
public function indexAction()
{
return array();
}
}
?>
And then i have my view which is i am sure correct. Please provide the detailed answer so that it can help me in clearing my concepts as well. Thanks !!!
If you take a look at the SecuredController from the AcmeDemoBundle, you'll see that the class declaration has this annotation at the start:
/**
* #Route("/demo/secured")
*/
class SecuredController extends Controller
{
// ...
This annotation prefixes all subsequent routes in the Controller class with /demo/secured. Therefore, you should write your own Controller using the same fashion:
/**
* #Route("/demo")
*/
class ContactusController extends Controller
{
// ...
See the docs concerning Route Prefixing for more information
alternatively, you could also leave the ContactusController class alone and modify your route import instead:
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
prefix: /demo
You're also using the same route name twice, which means Symfony will reload routes and overwrite anything you had before for _demo_contactus. Try the following:
routing.yml
_demo_contactus:
resource: "#AcmeDemoBundle/Controller/ContactusController.php"
type: annotation
ContactusController
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
/**
* #Route("/demo/contactus")
*/
class ContactusController extends Controller
{
/**
* #Route("/", name="_demo_contactus_home")
* #Template()
**/
public function indexAction()
{
return array();
}
}
Make sure you try both /demo/contactus and /demo/contactus/.. it may be looking at that slash

Categories