simple-php-router
simple-php-router copied to clipboard
Uncaught Pecee\SimpleRouter\Exceptions\NotFoundHttpException using Postman/Ajax
Hi, I'm using Simple-Router for quite a while and it's been marvelous, but I'm facing a problem with post requests, specially on my /api/login/request URL. For some reason it returns this error, which it's presented down below:
Fatal error: Uncaught Pecee\SimpleRouter\Exceptions\NotFoundHttpException: Route "/api/login/request/" or method "post" not allowed.
My project seems fine, all parameters are colected via URL (?email=&password)/URL-Encoding (www-x-form-urlencoded), but it simply doesn't do its job, in case, log in.
I wondered if I needed to specify any csrf_token parameter, but apparently that's not the issue.
I appreciate help on this matter, since I intent on using this library on another platforms, including administrative ones.
Edit
This is my route code.
Router::post('/api/login/request', 'Auth\Login@request');
The controller mentioned has its function labelled as public, in other words...
class Login {
public function request() {
// my code here
}
}
Hello,
can you please provide your index.php content and the namespace "header" of your Login class? You are importing SimpleRouter as Router I guess?
Some more information would be great so I can reproduce your issue and find a solution.
~ Marius
Hi, thank you for your patience!
My index.php is quite simple.
<?php
use Eden\System\RouterService;
include_once __DIR__ . '/../vendor/autoload.php';
ini_set('display_errors', 1);
$router = new RouterService;
$router->init();
In Eden\System\RouterService the code follows as...
<?php
namespace Eden\System;
use Eden\System\Middlewares\Authentication;
use Pecee\SimpleRouter\SimpleRouter as Router; // And yes! I'm importing SimpleRouter as Router, [as] stated.
class RouterService extends Router {
public function init() {
Router::setDefaultNamespace('\Eden\App\Controllers'); // Which are the folder that contains another subfolders with the stated controllers.
Router::group(['middleware' => Authentication::class], function (){
Router::post('/api/login/request', 'Auth\Login@request');
}
Router::start();
}
}
The mentioned middleware function is simple, only to aknowledge if the user is online or not, returning the user value if it is or the null value if not, and returning to the other middlewares that aren't featured on this issue.
By the way, it's important to mention that this code is labelled as private since it's a revisit of an old open-source project (about three years of existance), so I can't provide code that's sensitive to the project's security and its related core functions.
Again, thank you for your patience and comprehension.
Best regards, Akihito.
Edit
Sorry, I forgot to include the Login class namespace.
<?php
namespace Eden\App\Controllers\Login;
Edit 2
I managed to fix it. It was just the namespace as you said.
Thank you so much for your patience. You deserve the best, man!
Grateful, Akihito.
Hello,
I have to confirm my idea, but I think the route namespace is not used as a prefix if there is another namespace provided.
Case 1:
When no namespace is provided in the route the default namespace is used
Example: Login@request.
Case 2:
When you provide a namespace like Auth\Login it is like a absolute namespace not relative to the default namespace.
A solution for you could be to provide the class and method using an array [Login::class, 'request']. (Look it up in the Dokumentation)
When you are interested in a prefix namespace approach, please create a feature request in my fork. This repo is not supported anymore and my fork supports php 8.1 and I applied a lot of fixes but also did some breaking changes & added optional Input Validation.
I hope this helped you and if not I will try to find the issue. I am currently not at home so let me know if there is anything else.
~ Marius