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

  • Home
  • SEARCH
  • 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 8588819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:53:50+00:00 2026-06-11T22:53:50+00:00

How can I throw a 404 error code when a route does not exist?

  • 0

How can I throw a 404 error code when a route does not exist?

In phalcon after you set up your routing information – is there a way to check to see if a route coming in (from the user) matches any of the routes in your routes list? Then, if the route does not exist, throw a 404 error.

  • 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-11T22:53:51+00:00Added an answer on June 11, 2026 at 10:53 pm

    You can use something like this:

    public function main()
    {
        try {
    
            $this->_registerServices();
            $this->registerModules(self::$modules);
            $this->handle()->send();
    
        } catch (Exception $e) {
    
            // TODO log exception
    
            // remove view contents from buffer
            ob_clean();
    
            $errorCode = 500;
            $errorView = 'errors/500_error.phtml';
    
            switch (true) {
                // 401 UNAUTHORIZED
                case $e->getCode() == 401:
                    $errorCode = 401;
                    $errorView = 'errors/401_unathorized.phtml';
                    break;
    
                // 403 FORBIDDEN
                case $e->getCode() == 403:
                    $errorCode = 403;
                    $errorView = 'errors/403_forbidden.phtml';
                    break;
    
                // 404 NOT FOUND
                case $e->getCode() == 404:
                case ($e instanceof Phalcon\Mvc\View\Exception):
                case ($e instanceof Phalcon\Mvc\Dispatcher\Exception):
                    $errorCode = 404;
                    $errorView = 'errors/404_not_found.phtml';
                    break;
            }
    
            // Get error view contents. Since we are including the view
            // file here you can use PHP and local vars inside the error view.
            ob_start();
            include_once $errorView;
            $contents = ob_get_contents();
            ob_end_clean();
    
            // send view to header
            $response = $this->getDI()->getShared('response');
            $response->resetHeaders()
                ->setStatusCode($errorCode, null)
                ->setContent($contents)
                ->send();
        }
    }
    

    If you are using the Micro component you can use this:

    $app->notFound(
        function () use ($app) {
            $app->response->setStatusCode(404, "Not Found")->sendHeaders();
            echo 'This is crazy, but this page was not found!';
        }
    );
    

    Of course you can use the suggestions that others posted regarding the .htaccess file, but above is how you do it in Phalcon without touching anything else.

    There is also a new feature coming down the pipeline, regarding a default error handler that would process errors in Phalcon (or overriden if necessary).

    Credits to Nesbert for the gist

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

when sending packet to openfire using Smack, i get error remote-server-not-found(404) . Can anyone
As Error and Exception are subclass of throwable class, we can throw any error,
It seems illogical that this statement could throw a 404 error. Any ideas? users.create_logout_url(self.request.uri)
I am managing a wordpress blog and want to throw a 404 error whenever
I am wondering why my MediaPLayerLauncher always throw an error 404 from the server.
I'm trying to figure out if the boost::multi_array constructor or resize method can throw
Can I throw an exception on the SQL 2005 server so that I can
I noticed that you can't throw an exception in a destructor. So my question
For many methods in .NET, the exceptions they can potentially throw can be as
Ok, I've been told this problem: Why can you throw a pointer to 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.