# How to return HTTP status codes generated in route?

**URL:** <https://discourse.slimframework.com/t/how-to-return-http-status-codes-generated-in-route/756>\
**Category:** Questions\
**Created:** [September 25, 2016, 2:34pm UTC](https://discourse.slimframework.com/t/how-to-return-http-status-codes-generated-in-route/756 "2016-09-25T14:34:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NotionCommotion](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@NotionCommotion](https://discourse.slimframework.com/u/NotionCommotion)\
**Post date:** [September 25, 2016, 2:34pm UTC](https://discourse.slimframework.com/t/how-to-return-http-status-codes-generated-in-route/756/1 "2016-09-25T14:34:47Z")

</div>

Hello all! New to Slim, and first time new to here. I have a primary question and a general question.

1. Primarily, I would like to allow SomeResource::doSomething() to validate its scope of work, and if there is an error, return the HTTP status code back to Slim so it might be returned to the client. I tried to have SomeResource::doSomething() execute `http_response_code(400)`;, and have slim check the code and forward it, but slim still sees 200. How is this accomplished?
2. Generally, can anyone give a quick review of the below code, and provide any constructive criticism.

Thanks!

```
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require __DIR__.'/../vendor/autoload.php';
spl_autoload_register(function ($classname) {
    $parts = explode('\\', $classname);
    require __DIR__."/../classes/".end($parts).".php";
});

$settings=[
    'settings'=>[
        'db'=>parse_ini_file( __DIR__.'/../../db.ini',true)
    ],
    'displayErrorDetails'=> true,
    'determineRouteBeforeAppMiddleware' => true //Not sure if this is necessary
];
$container = new \Slim\Container($settings);

$container['pdo'] = function ($container) {
    $cfg = $container->get('settings')['db'];
    return new \PDO($cfg['dsn'], $cfg['user'], $cfg['password']);
};

$container['someResource'] = function ($container) {
    return new someResourceMapper($container->get('pdo'));
};

$app = new \Slim\App($container);

$app->get('/', function($request, $response) {
    return $response->withJson($this->get('someResource')->doSomething())->withStatus($response->getStatusCode());
});

$app->add(function(Request $request, Response $response, $next) {
    // Use middleware for authentication. Be sure to put as last route so it is executed first
    if (invalidUser()) {
        return $response->withJson(\MyApp\ErrorResponse::invalidUser(), 401);
    }
    // If desired, add something to the container
    $this['account'] = [123];
    return $next($request, $response);
});

// Forgot why I am doing this...
$app->options('/{routes:.+}', function ($request, $response, $args) {
    return $response;
});

$app->run();
```

---

<div class="post-metadata">

**Author:** ![nanaksr](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@nanaksr](https://discourse.slimframework.com/u/nanaksr)\
**Post date:** [September 29, 2016, 6:16pm UTC](https://discourse.slimframework.com/t/how-to-return-http-status-codes-generated-in-route/756/2 "2016-09-29T18:16:12Z")

</div>

hello,

This might help. I use my own technique by utilizing the “throw new Exception”. and error handler on link: [http://www.slimframework.com/docs/handlers/error.html](http://www.slimframework.com/docs/handlers/error.html)

My code on depedencies :  
$container = $app-\>getContainer();

$container[‘notFoundHandler’] = function ($c) {  
return function ($request, $response) use ($c) {  
throw new Exception(‘Api Not Found’, 404);  
};  
};

$container[‘notAllowedHandler’] = function ($c) {  
return function ($request, $response) use ($c) {  
throw new Exception(‘Method Not Allowed’, 405);  
};  
};

$container[‘errorHandler’] = function ($c) {  
return function ($request, $response, $exception) use ($c) {  
$data = [  
‘code’ =\> $exception-\>getCode(),  
‘message’ =\> ($exception-\>getCode() != 200) ? $exception-\>getMessage() : ‘OK’,  
‘data’ =\> json\_decode($exception-\>getMessage())  
];  
return $c[‘response’]-\>withStatus($exception-\>getCode())  
-\>withJson($data);  
};  
};

and code on my app :  
throw new Exception(json\_encode($yourArray), 200); --\> if success  
throw new Exception(“your message”, 404); --\> if error

you can put it another code and result.

I hope this helps.

---

<div class="post-metadata">

**Author:** ![NotionCommotion](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@NotionCommotion](https://discourse.slimframework.com/u/NotionCommotion)\
**Post date:** [September 30, 2016, 11:59am UTC](https://discourse.slimframework.com/t/how-to-return-http-status-codes-generated-in-route/756/3 "2016-09-30T11:59:46Z")

</div>

Thanks nanaksr,

So, you use exceptions for both success and errors, and then you catch them?

Where do you catch tme?
