Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set a different 404 handler based on the subroute #59

Closed
artyuum opened this issue Jan 1, 2018 · 6 comments
Closed

Ability to set a different 404 handler based on the subroute #59

artyuum opened this issue Jan 1, 2018 · 6 comments

Comments

@artyuum
Copy link
Contributor

artyuum commented Jan 1, 2018

Hi,
example :

// Create Router instance
$router = new \Bramus\Router\Router();

// Set the default namespace for all controllers
$router->setNamespace('\App\Controllers');

// Define routes
$router->mount('/website', function() use ($router) {

    $router->set404('ErrorController@notFoundWebsite');

    $router->get('/', 'WebsiteController@index');

});

$router->mount('/api', function() use ($router) {

    $router->set404('ErrorController@notFoundApi');

    $router->get('/posts', 'ApiController@posts');
    $router->get('/posts/(\d+)', 'ApiController@post');

});

// Run it!
$router->run();

In this example, in case of error 404 for the api subroute, a JSON response will be returned.
For the website subroute, a simple HTML page will be returned.

Thanks for your time.

@bramus
Copy link
Owner

bramus commented Jan 25, 2019

I'll gladly accept a PR that provides this.

@acicali
Copy link
Contributor

acicali commented Feb 14, 2020

This can be accomplished with a very minor change to the beginning of Router->mount()

    public function mount($baseRoute, $fn)
    {
        // Bail if this mount does not match the URI
        if(strpos($this->getCurrentUri(), $baseRoute) !== 0){
            return false;
        }

        // ...

This change may also resolve #55 since it stops arbitrary code from being run within the mount()'d callable when the route does not match. It is not compatible with #75, but that feature does not exist anyway.

@floralsystems
Copy link

This solution means that calls to $router->set404() after $router->mount() would overwrite the mounted 404. It's not ideal.

@sumanthratna
Copy link

Obviously not the best solution, but this could work:

  • create a GET route for /404, and this route should accept a get parameter giving the path of the initially requested page
  • whenever the user requests an invalid page, use header or another method to redirect to /404?route=[THE ROUTE THE USER REQUESTED]
  • when dealing with the GET route to /404, you can use a simple if statement to see if the route GET parameter starts with api or not
  • show different 404 pages based on the value of the route variable

@acicali
Copy link
Contributor

acicali commented Jul 24, 2020

I advise against redirection as a means to serve 404 responses. It breaks the web. Perhaps under certain conditions, an action by another request might make that URL no longer a 404 (e.g. user created CMS pages). By redirecting, you've taken away the user's ability to reload the page and get the expected response. Instead, they'll have to go back a page.

@uvulpos
Copy link
Collaborator

uvulpos commented Apr 11, 2021

I missed that Issue sorry → #144 #145
With my Pull Request, you could say:

$router->set404('ErrorController@notFoundWebsite');
$router->set404('/api(/.*)?', 'ErrorController@notFoundApi');

@artyuum artyuum closed this as completed Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants