Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:58:03+00:00 2026-06-10T06:58:03+00:00

I have this problem with Slim framework . I have class Template that have

  • 0

I have this problem with Slim framework. I have class Template that have render method and I want for Slim to render objects of this class if they are returned by route handlers

$app->get('/test', function() {
    return new Template('main', function() use ($error) {
        return array(
            'content' => "Hello"
        );
    });
});

it work I created child class (in System.php)

class System extends Slim {
   function __constructor() {
       Slim::__construct();
   }
   private function auto_render_fun($callable) {
        $app = $this;
        return function() use ($callable, $app) {
            $args = func_get_args();
            $ret = call_user_func_array($callable, $args);
            if ($ret instanceof Template) {
                //render Template - Slim ignore return value
                $app->response()->body($ret->render());
            }
        };
    }
    protected function mapRoute($args) {
        $last = count($args)-1;
        $args[$last] = $this->auto_render_fun($args[$last]);
        return Slim::mapRoute($args);
    }
}

I wanted to do the same thing with notFound

$app->notFound(function () use ($app) {
    $response = $app->response();
    $response['Content-Type'] = 'text/html';
    return new Template('main', function() {
        return array('content' => new Template('error_404', null));
    });
});

so I’ve overwritten notFound function to wrap Closure and render it’s return value

First I try to use smaller code

public function notFound($callable = null) {
    if (!is_null(($callable))) {
        $this->router->notFound($this->auto_render_fun($callable));
    } else {
        Slim::notFound();
    }
}

I also try this (copy and modify old code).

public function notFound($callable = null) {
    if ( !is_null($callable) ) {
        $this->router->notFound($this->auto_render_fun($callable));
        //            $this->router->notFound($callable);    // old line
    } else {
        ob_start();
        $customNotFoundHandler = $this->router->notFound();
        if ( is_callable($customNotFoundHandler) ) {
            call_user_func($customNotFoundHandler);
        } else {
            call_user_func(array($this, 'defaultNotFound'));
        }
        $this->halt(404, ob_get_clean());
    }
}

but the reason why it didn’t work is that it trow Slim_Exception_Stop that’s suppose to be cached by Slim here is that line for code that call $this->notFound();
https://github.com/codeguy/Slim/blob/master/Slim/Slim.php#L1160
it’s inside try..catch.

Here is stack trace (I cached it inside notFound function – but it should be handled in Slim class).

Slim_Exception_Stop in file libs/Slim/Slim/Slim.php at 862
0: Slim->stop() in libs/Slim/Slim/Slim.php at 882
1: Slim->halt(integer, string) in libs/System.php at 187
2: System->notFound() in libs/Slim/Slim/Slim.php at 1161
3: Slim->call() in libs/Slim/Slim/Middleware/Flash.php at 84
4: Slim_Middleware_Flash->call() in libs/Slim/Slim/Middleware/MethodOverride.php at 91
5: Slim_Middleware_MethodOverride->call() in libs/Slim/Slim/Middleware/PrettyExceptions.php at 65
6: Slim_Middleware_PrettyExceptions->call() in libs/Slim/Slim/Middleware/PrettyExceptions.php at 65
7: Slim_Middleware_PrettyExceptions->call() in libs/Slim/Slim/Slim.php at 1098
8: Slim->run() in index.php at 573
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:58:05+00:00Added an answer on June 10, 2026 at 6:58 am

    Ok, I found the reason, exception was handled as it should but there was not content. I wrongly assume that it’s something with Classes, but simply the Slim code have bugs.

    public function halt( $status, $message = '' ) {
        $this->cleanBuffer();
        $this->response->status($status);
        $this->response->body($message);
        $this->stop();
    }
    

    function overwrite data that you use in the handler like this

    $app->notFound(function() {
       $app->response()->body('Error');
    });
    

    won’t work, function not found should look like this

    public function notFound($callable = null) {
        if (!is_null(($callable))) {
            $this->router->notFound($this->auto_render_fun($callable));
        } else {
            $customNotFoundHandler = $this->router->notFound();
            if (is_callable($customNotFoundHandler)) {
                call_user_func($customNotFoundHandler);
            } else {
                call_user_func(array($this, 'defaultNotFound'));
            }
            $this->response->status(404);
            $this->stop();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem: I want to generate a new source code file from
I have this problem since I'm beginning in OOP programming I want to close
I have this problem in my SlashDot Menu. I want to change the picture
I have this problem that I can ref a button for control it. I
i have this problem: I have created a bash script that performs some tasks.
i have this problem: starting from an empty list (0 elements) i want check
I have this problem that drives me mad, so I am here to ask
I have this problem to solve that I have no idea how to do
I have this problem that I can't seem to find a solution for... I
I have this problem with UISearchDisplayController . I want to use it in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.