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.
Related
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 ?
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^^)
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.
I'm getting a 404 error when trying to access a route linked to a controller action.
I have the route defined like this in my routes.php file.
Route::controller('error', 'ErrorsController');
The ErrorsController class looks as follows.
class ErrorsController extends BaseController {
public function __construct()
{
// vacio
}
public function getIndex()
{
return View::make('error.accessdenied');
}
public function getAccessDenied()
{
return View::make('error.accessdenied');
}
}
I have a view with a link to chek if it is working properly. The link is created as follows
{{ HTML::linkAction('ErrorsController#getAccessDenied', 'Error') }}
When I click on the link the page moves to the URL 'mytestdomain.com/error/access-denied' returning an 404 error, but when I access the URL 'mytestdomain.com/error' it works perfectly.
Any idea on what I'm doing wrong?
EDIT:
Running the command php artisan routes these are the routes pointing to ErrorsController:
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
| | GET|HEAD error/index/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getIndex | | |
| | GET|HEAD error | | ErrorsController#getIndex | | |
| | GET|HEAD error/access-denied/{one?}/{two?}/{three?}/{four?}/{five?} | | ErrorsController#getAccessDenied | | |
| | GET|HEAD|POST|PUT|PATCH|DELETE error/{_missing} | | ErrorsController#missingMethod | | |
+--------+------------------------------------------------------------------------------------------------+------+--------------------------------------+----------------+---------------+
Only the sencond and the fourth ones are working.
It looks as though specifying the route in the way you have won't work. This type of routing only works for RESTful requests. See >http://laravel.com/docs/4.2/controllers#restful-resource-controllers>.
You might have to explicitly specify the route using Route::get/post.
Somehow I found the problem.
For some reason, my apache server doesn't rewrite mytestdomain.com/error/ * route. Probably is something related with the word error and the apache module mod_rewrite.
Anyway, defining the route as follows solves the problem.
Route::controller('fail', 'ErrorsController');
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.