How to return HTTP status codes generated in route?

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.


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";

    '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;



This might help. I use my own technique by utilizing the “throw new Exception”. and error handler on link:

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())

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.

1 Like

Thanks nanaksr,

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

Where do you catch tme?