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

How to exit in the middle of function with error code?

ilan's Avatar

ilan

20 Dec, 2015 09:35 AM

Hello,
I am trying to implement my first post function with Slim 3.0.0.
e.g. $app->post('/register',function (ServerRequestInterface $request, ResponseInterface $response, $args)
However, I am facing some problems:
1. How can I exit with status 400 in the middle of the function with error text?
2. What is preferred way to return at the end of this function?
3. Can you forward me to a detailed and more complicated usage example of using Slim 3.0.0.
Many thanks in advance
Ilan.

  1. 1 Posted by Rob Allen on 20 Dec, 2015 09:37 AM

    Rob Allen's Avatar
    1. return $response->withStatusCode(400);
    2. return $response;
    3. https://github.com/akrabat/slim-bookshelf

    Regards,

    Rob...

  2. 2 Posted by ilan on 20 Dec, 2015 08:38 PM

    ilan's Avatar

    I am sorry to interrupt you again.
    I had an old function that have been called from 'post function:
    function validateEmail($email) {
        $app = \Slim\Slim::getInstance();
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $response["error"] = true;
            $response["message"] = 'Email address is not valid';
            $app->status(400);
            $app->contentType('application/json');
            echo json_encode($response);
            $app->stop();
        }
    }
    How can I write something similar in Slim 3.0.0?
    Many Thanks
    Ilan.

  3. 3 Posted by Rob Allen on 21 Dec, 2015 09:48 AM

    Rob Allen's Avatar

    Throw an exception and catch it in the route callable. Within the route callable, return a $response with the status set to 400 and the appropriate body.

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