We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Handling not found action

Hello,

I could not find or create a solutions for this: I want to handle a "not found" action of a specific controller.

My problem is: I created a controller to handle css files and, when a file does not exist, I would like to return a specific message in a comment block.

My code is something like this:

// Setting router
$router->add('/css/([a-zA-Z0-9]+).(css|less)', [
   'controller' => 'css',
   'action' => 1,
   'format' => 2
]);

// Css Controller
class CssController extends \Phalcon\Mvc\Controller
{
   public function beforeExecuteRoute()
   {
      $this->response->setContentType('text/css', 'UTF-8');
   }

   public function notfoundAction() {}

   public function somefileAction()
   {
      $this->view->color = '#aaa';

      // ...
   }
}

// views/css/notfound.css
/* File Not Found */

// views/css/somefile.css
body {
    background: {{ color }};
}

I'm using volt engine to handle .css and .less extensions.

In this way, I get the follow answer from /css/somefile.css:

body {
   background: #aaa;
}

And I would like to get the follow answer always when I try to access some file (in this case, action) that does not exists:

/* File Not Found */

There is some way to set a default (or notfound) action to a specific controller? If not, does someone have a suggest to solve it?

Thank you

[]s

Have you tried using?

public function beforeNotFoundAction()
{
    /* File Not Found */
}

Hope that help...

Sorry, I forgot to say I tried it, but didn't work:

// Css Controller
class CssController extends \Phalcon\Mvc\Controller
{
   public function beforeExecuteRoute()
   {
      $this->response->setContentType('text/css', 'UTF-8');
   }

   public function beforeNotFoundAction()
   {
      echo "Message";
      exit;
   }

   public function somefileAction()
   {
      $this->view->color = '#aaa';

      // ...
   }
}

Thanks