The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim 3 - Throw not found exception

Chris's Avatar

Chris

23 May, 2015 06:28 AM

Hello. How to throw not found exception in slim 3?

  1. 1 Posted by Sylfel on 16 Sep, 2015 07:27 AM

    Sylfel's Avatar

    I have same problem.

    In slim3, NotFoundException are throwed (and capture by "notFoundHandler") when no matching route was found.

    But if a route is found, (ie "/user/toto" for pattern "/user/{toto}") how to throw a NotFoundException if "toto" is not a user ?

    Thanks

  2. 2 Posted by Sylfel on 04 Nov, 2015 01:08 PM

    Sylfel's Avatar

    I respond to myself : "middleware is magic"

    I just add a middleware :

    $app->add(function ($request, $response, $next) {
        try {
            $response = $next($request, $response);
        }
        catch(Slim\Exception\NotFoundException $e) {
            $notFoundHandler = $this->get('notFoundHandler');
            return $notFoundHandler($request->withAttribute('message', $e->getMessage()), $response);
        }
        return $response;
    });
    

    And now, anywhere in my app, i can throw a NotFoundException :

    throw new NotFoundException("Product not found");
    

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac