Why is php symfony framework method not used (controller)? - php

Any ideas what can be wrong? PHP Storm says, that homepage is unused. On webside there is still symfony homepage so it doesnt work even on site.
routes.yaml
index:
path: /
controller: App\Controller\QuestionController::homepage
QuestionController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class QuestionController
{
public function homepage()
{
return new Response('What a bewitching controller we have conjured!');
}
}
Update:
/**
* #Route("/")
*/
also doesnt work work homepage()

in config/routes create file annotation.yaml
with such code:
controllers:
resource: ../../src/Controller/
type: annotation
that's all, enjoy :)

Related

Routes are declared in routes.yaml but symfony don't found them. Even when i try using annotations

Ihave a symfony app that ignore the roytes i can declare.
For exemple, i have this lines of code in routes.yaml :
app_titi
path: /titi
methods: ['GET']
defaults:
_controller: 'App\Controller\TitiController::index'
I do a cache:clear and when i try to watch the result in my browser, no route found.
The controller exists and have the right name.
My context is Symfony 6.2, PHP 8.1, runing in Docker containers.
I tryed to create a new controller, i declared it in routes.yaml, same results.
I tryed to create a controller but this time using annotations, same results.
When i ask the command router:debug, symfony console returns an empty results.
Thanks for helping!
For a better answer please provide your controller path and code example.
In Symfony 6.2, by default, you do not have to change routes.yaml since all route declared in controller are auto detected if you keep this inside routes.yaml.
Here is my routes.yaml for example :
controllers:
resource: ../src/Controller/
type: annotation
kernel:
resource: ../src/Kernel.php
type: annotation
Then inside the controller
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class HomeController extends AbstractController
{
// This not annotation but php attribute, i advise you to use it instead
#[Route('/', name: "home")]
public function home(): Response
{
return $this->render('website/index.html.twig');
}
}
Read this to understand why symfony now use php attribute :
https://symfony.com/blog/new-in-symfony-5-2-php-8-attributes
#ThomasL
Hi, here is my routes.yaml :
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute
app_titi:
path: /titi
methods: ['GET']
defaults:
_controller: 'App\Controller\TitiController::index'
Others routes are now declared in attributes.
This is my Titi controller :
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class TitiController extends AbstractController
{
#[Route('/titi', name: 'app_titi')]
public function index(): Response
{
return $this->render('titi/index.html.twig', [
'controller_name' => 'TitiController',
]);
}
}

404 not found , Symfony routing path problem

i create a controller pageController.php :
<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
class pageController {
public function index()
{
return new Response('<html><body>hello...</body></html>');
}
public function contactAction()
{
return new Response('<html><body>contact...</body></html>');
}
}
and here is the routes.yml
index:
path: /
controller: App\Controller\pageController::index
contact:
path: /contact
controller: App\Controller\pageController::contactAction
the index works fine, but the contact doesn't work!
Note: when I changed the path of index from "/" to "/index", it doesn't work anymore, it shows 404 not found
I don't want to use annotations until i want to fix this
contact:
path: /contact
controller: App\Controller\pageController::contact
Symfony will look for your contactAction you don't need to mention it in your YML
Ps: you don't call a route by a route name /index wont work but you call it by the path /
I don't know which version of symfony you are using, but if it is symfony 2.x then you should name your index method as indexAction (exactly like you have in contact).
routes.yml:
index:
path: /
controller: App\Controller\pageController::indexAction
controller:
public function indexAction()
{
return new Response('<html><body>hello...</body></html>');
}
You should also make sure that your routes.yml are properly loaded.

My first controller in symfony

// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number")
*/
public function numberAction()
{
$number = rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
I'm new of Symfony framework. I tried this simple code without any result.
That's the server response:
No route found for "GET /lucky/number" 404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException ยป
I don't now why the Default controller use the annotation and I can see the homepage of my Symfony application.
I think you need to import your annotation routes. Symfony will scan locations you mention in app/config/routing.yml.
# app/config/routing.yml
lucky:
resource: "#AppBundle/Controller/LuckyController.php"
type: annotation
See Symfony docs for more details
Also try clearing the cache, just to be sure.
You're missing an important part of the route and its name:
/**
* #Route("/lucky/number", name="lucky_number")
*/
More information: http://symfony.com/doc/current/book/routing.html

FileLoaderException: Cannot import resource "mycontroller" from "C:/wamp/www/Symfony/app/config\routing.yml

I just followed the tutorial about single route on github for this bundle : https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/5-automatic-route-generation_single-restful-controller.md
But I got this error when I'm trying to load my home page:
"FileLoaderException: Cannot import resource "ADC\OgppBundle\Controller\OgppRestController" from "C:/wamp/www/Symfony/app/config\routing.yml" (Class could not be determined for Controller identified by "ADC\OgppBundle\Controller\OgppRestController".)
I know this is some basic stuff but I cannot make it work. Here is the code :
app/config/routing.yml
adc_rest:
resource: ADC\OgppBundle\Controller\OgppRestController
type: rest
ADC\OgppBundle\Controller\OgppRest.php
namespace ADC\OgppBundle\Controller;
use FOS\RestBundle\Controller\Annotations\View;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class OgppRestController extends Controller
{
public function getProfilesAction()
{
} // "get_profiles" [GET] /profile/all
public function getProfileAction($id)
{
} // "get_profile" [GET] /profile/{id}
}
EDIT : I tried to clear the cache in dev environment but when I do it I have the same issue.
Hope this helps
I don't know how FOSRestBundle works (I've never used it), but I think your controller file name is wrong, it should be OgppRestController.php, not OgppRest.php.

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