Symfony 3.4: Route Matches but no route found in url - php

I've just created a new controller as i usually do but, this time I got a problem with routing.
In profiler route matches but I can't reach them and get No route found for "GET /prezzi/listino"
Controller
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
class PricesController extends Controller {
/**
* #Route("/prezzi/listino", name="prezzi_listino")
*/
public function pricesListAction()
{
$list = $this->getDoctrine()->getRepository('AppBundle:Prices')->findAll();
return $this->render('prices/list.html.twig', [
'items' => $list
]);
}
}
Debug Router
$ php bin/console debug:router
----------------------------------- ---------- -------- ------ -------------------------------------------------------
Name Method Scheme Host Path
----------------------------------- ---------- -------- ------ -------------------------------------------------------
[..]
prezzi_listino ANY ANY ANY /prezzi/listino
[..]
----------------------------------- ---------- -------- ------ -------------------------------------------------------
Router Match
$ php bin/console router:match --method GET /prezzi/listino
[OK] Route "prezzi_listino" matches
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | prezzi_listino |
| Path | /prezzi/listino |
| Path Regex | #^/prezzi/listino$#sD |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | ANY |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: AppBundle:Prices:pricesList |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
| Callable | AppBundle\Controller\PricesController::pricesListAction |
+--------------+---------------------------------------------------------+
Any ideas of where is the error? I think is a distraction cause I haven't see this problem.

Since I've tried to add the route in another controller that actually work, and doesn't seems to work, I've just come to the conclusion that I can't add any new route so it's meaning that is a cache error.
Running php bin/console cache:clear --env=dev --no-warmup solve the problem.

Related

Symfony page not found (Symfony 3)

I am new in Symfony and i am working on existing project. I have created crud with doctrine:generate:crud, but application returns me 404 page not found. I was debugging it with debug:router and router:match and everything was okay.
Here is my controller
<?php
namespace AppBundle\Controller\Backend;
use AppBundle\Controller\BackendController;
use AppBundle\Entity\CsobApiUsers;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
/**
* CsobApiUsers controller.
*
* #Route("csobApiUsers")
*/
class CsobApiUsersController extends BackendController
{
/**
* Lists all csobApiUser entities.
*
* #Route("/", name="csobapiusers_index")
* #Method("GET")
*/
public function indexAction()
{
die(var_dump("x"));
$em = $this->getDoctrine()->getManager();
$csobApiUsers = $em->getRepository('AppBundle:CsobApiUsers')->findAll();
return $this->render('csobapiusers/index.html.twig', array(
'csobApiUsers' => $csobApiUsers,
));
}
Controller is as same as another controller that works correctly.
Here is my router:match
php bin/console router:match /backend/csobApiUsers/
[OK] Route "csobapiusers_index" matches
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | csobapiusers_index |
| Path | /backend/csobApiUsers/ |
| Path Regex | #^/backend/csobApiUsers/$#s |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | GET |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: AppBundle:Backend\CsobApiUsers:index |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
Here is debug:router for my controller
csobapiusers_index GET ANY ANY /backend/csobApiUsers/
csobapiusers_new GET|POST ANY ANY /backend/csobApiUsers/new
csobapiusers_show GET ANY ANY /backend/csobApiUsers/{id}
csobapiusers_edit GET|POST ANY ANY /backend/csobApiUsers/{id}/edit
csobapiusers_delete DELETE ANY ANY /backend/csobApiUsers/{id}
I calling https://my-address.com/backend/csobApiUsers/
Does anybody know where can be problem? Thanks
I think you're missing slash in controller route annotation, check here https://symfony.com/blog/new-in-symfony-3-4-prefix-all-controller-route-names
#Route("/csobApiUsers")
Did you try changing your indexAction() to index() instead ?

Symfony5 throws 404 in browser, but route is found in cli

Iam using react+symfony with webpack. Everything works with simple url eg. one slash in url ("/aboutus","/moodle") but when i try access route with multiple slashes("/admin/users") i get NotFoundResource Error. But with router:matchi will find it.
Server log:
|WARN | SERVER GET (404) /admin/users
|ERROR| REQUES Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /admin/users"" at /home/rtkpf/Programming/Niners/vendor/symfony/http-kernel/EventListener/RouterListener.php line 136
router:match /admin/users
(master) [1]> bin/console router:match /admin/users
[OK] Route "index" matches
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | index |
| Path | /{reactRouting} |
| Path Regex | {^/(?P<reactRouting>.+)?$}sDu |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | GET |
| Requirements | reactRouting: .+ |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: App\Controller\UserController::index() |
| | reactRouting: NULL |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
| | utf8: true |
+--------------+---------------------------------------------------------+
Component:
class DefaultController extends AbstractController
{
/**
* #Route("/{reactRouting}", name="index", defaults={"reactRouting": null}, methods="GET", requirements={"reactRouting":".+"})
*/
public function index()
{
return $this->render('default/index.html.twig');
}
}
Has someone experienced this before? Where can be a mistake?
P.S.: Ia m using symfony server
It says App\Controller\UserController::index() in router:match while your controller shows DefaultController, is this a possible problem?
There could also be a priority issue, since 5.1 you can use priorities in routing (https://symfony.com/blog/new-in-symfony-5-1-route-annotations-priority), perhaps try adding priority=-10, in the router annotation to prioritize it lower (0 is default) or priority=10, in case you want to increase the priority somewhere else.

Why does route requirements works for my first route and not for my second one?

I got this issue :
My first route disallow me of using any character that isn't a number (from the regex), this is perfectly working (tell me if you need screen of the result)
But the second one let me use any character as an id, I don't get why, I've tried clearing cache (and many other things) but it stills allow me to use alpha characters.
I have theses two routes currently :
<?php
// src/Controller/AdvertController.php
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* #Route("/advert")
*/
class AdvertController extends AbstractController
{
/**
* #Route("/{page}", name="oc_advert_index", requirements={"page" = "\d+"}, defaults={"page" = 1})
*/
public function index(Environment $twig, $page)
{
$content = $twig->render('Advert/index.html.twig', ['page' => $page, 'name' => 'alex']);
return new Response($content);
}
/**
* #Route("/view/{id}", name="oc_advert_view", requirements={"id" = "\d+"})
*/
public function view(Environment $twig, $id)
{
$content = $twig->render('Advert/view.html.twig', ['id' => $id, 'name' => 'alex']);
return new Response($content);
}
}
My templates are basic Twig templates (displaying either "id" or "page")
I've tried to look for hidden space and something but I can't find where is the difference (excluding the names "id" and "page")
EDIT1 : here what's I got when executing php bin/console router/match /advert/view/abc:
[OK] Route "oc_advert_view" matches
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | oc_advert_view |
| Path | /advert/view/{id} |
| Path Regex | #^/advert/view/(?P<id>[^/]++)$#sDu |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | ANY |
| Requirements | NO CUSTOM |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: App\Controller\AdvertController::view |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
| | utf8: true |
+--------------+---------------------------------------------------------+
It seems that the "Requirements" are not detected/used for some reason, does someone know why ?
EDIT2: Here what I got when doing php bin/console router:match /advert/123
[OK] Route "oc_advert_index" matches
+--------------+---------------------------------------------------------+
| Property | Value |
+--------------+---------------------------------------------------------+
| Route Name | oc_advert_index |
| Path | /advert/{page} |
| Path Regex | #^/advert(?:/(?P<page>\d+))?$#sDu |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | ANY |
| Requirements | page: \d+ |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: App\Controller\AdvertController::index |
| | page: 1 |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
| | utf8: true |
+--------------+---------------------------------------------------------+
Here we can see that it detects the regex, it feels like this is a typo error but I have copy/pasted at least 3 times, very weird :/
FINAL EDIT:
Thank you to every person who tried to find the issue, I learned a few things so I am very thankful (and sorry for the time waste^^)
RESOLVED : There were conflicts between config/routes.yaml and my routes, I had to delete the duplicated route. (I followed a course where we first declare it in config/routes.yaml and then we declare routes with annotations, forgot the step of deleting content of config/routes.yaml)
Thank you to every person who tried to find the issue, I learned a few things so I am very thankful (and sorry for the time waste^^)

Laravel 5.1 adding middleware to resource route

so I have been trying to use middleware with my route resource and having trouble making it work.
Here is my routes setup:
Route::group(['prefix' => 'api','middleware' => 'locationRouteValidator'], function()
{
Route::resource('location', 'LocationController');
});
and route seems to be setup properly:
php artisan route:list
+--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+
| | GET|HEAD | / | | Closure | |
| | GET|HEAD | api/location | api.location.index | App\Http\Controllers\LocationController#index | locationRouteValidator |
| | POST | api/location | api.location.store | App\Http\Controllers\LocationController#store | locationRouteValidator |
| | GET|HEAD | api/location/create | api.location.create | App\Http\Controllers\LocationController#create | locationRouteValidator |
| | DELETE | api/location/{location} | api.location.destroy | App\Http\Controllers\LocationController#destroy | locationRouteValidator |
| | PATCH | api/location/{location} | | App\Http\Controllers\LocationController#update | locationRouteValidator |
| | GET|HEAD | api/location/{location} | api.location.show | App\Http\Controllers\LocationController#show | locationRouteValidator |
| | PUT | api/location/{location} | api.location.update | App\Http\Controllers\LocationController#update | locationRouteValidator |
| | GET|HEAD | api/location/{location}/edit | api.location.edit | App\Http\Controllers\LocationController#edit | locationRouteValidator |
+--------+----------+------------------------------+----------------------+-------------------------------------------------+------------------------+
so now I create the middleware :
php artisan make:middleware locationRouteValidator
and leave the default code, which is :
public function handle($request, Closure $next)
{
return $next($request);
}
and just for testing, in my controllers show method, I echo out the passed id like so:
public function show($id)
{
//
echo "show ".$id;
}
so now I expect that when I visit /public/api/location/abcd it should display:
show abcd or when I visit /public/api/location/1234 it should display show 1234 after which I intended to modify the middleware to allow only numeric values to be passed into {location}.
But If I just run with the default middleware code, the page returns white without displaying anything. I remove the middleware from the route, and it displays the text as expected.
I know I could attach the middleware to the controller instead, but I thought of attaching it in the route instead so that I could write and apply some common middleware by using the route's group feature, which should be possible, right?
Where do you guys think I am going wrong? Thanks in advance for looking!
Check your \app\http\kernel.php file to see if you have registered the middleware as a route middleware.

Laravel Using controller Route

i have resource in route and that work correctly and i want to change that to Route::controller.
but after define that i get error in php artisan route :
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
| | GET index | index | Closure | | |
| | GET admin/index | dashboard | Closure | | |
| | GET logout | logout | Closure | | |
| | POST auth | auth | Closure | csrf | |
| | GET login | login | Closure | | |
| | GET admin/admin/profile/{_missing} | | ProfileController#missingMethod | | |
+--------+------------------------------------+-----------+---------------------------------+----------------+---------------+
my Current route is:
Route::resource('profile' , 'ProfileController', array('as'=>'profile') );
and i want to change that to :
Route::controller('admin/profile', 'ProfileController', array('index'=>'profile.index') );
how to resolve this problem?
This is not an error, Resource and Controller routes are completely different things.
Resource routes have a predefined list of routes (index, create, store, delete, update). If you don't have the method set in your controller it will still work, unless someone hit that route.
Controller routes relies on your controller methods:
public function getIndex() {}
public function getCreate() {}
public function postStore() {}
Methods names are predefined as
<http method><your action name>()
If those methods are not present in your controller, Laravel will not show them in your routes list.
So, just create a
public function getIndex() {}
In your controller and run
php artisan route
Again.
Use :
Route::resource('profile, 'ProfileController', array('as' => 'profile', 'names' => array('index' => 'profile.index')));
Instead of either the routes above.

Categories