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

Phalcon throw new \Exception("Error msg"); to view module

Hi all, My application using multi module. I want show error msg to view of module default: ex: home module, index controller, error action. I know this can work dispatcher.

Help me to complete code for this

public function editAction($id) {
    $model = new Products();
    $test = $model->findFirst($id);
    if($test == false){
        throw new \Exception("The system was deactivated.");
    }
}

maybe you need this

public function editAction($id) {
    $model = new Products();
    $test = $model->findFirst($id);
    if($test == false){
         $this->flashSession->error("The system was deactivated.");
         return $this->response->redirect('controller/action');
    }
}

in view

<?php $this->flashSession->output() ?>

It will change the url

maybe you need this

public function editAction($id) {
   $model = new Products();
   $test = $model->findFirst($id);
   if($test == false){
        $this->flashSession->error("The system was deactivated.");
        return $this->response->redirect('controller/action');
   }
}

in view

<?php $this->flashSession->output() ?>


12.2k

@thanhansoft

@ch3k1 has it right, you need to redirect. If you want to redirect to a different module, you use:


$this->response->redirect(
            [
                'module' => 'your_module',
                'controller' => 'your_controller',
                'action' => 'your_action',
            ]
);

# or for a named router

$this->response->redirect(
            [
                'for' => 'named-route'
            ]
);

If you want to do it via Exceptions, to continue your code:


public function editAction($id) {
  try {
      $model = new Products();
      $test = $model->findFirst($id);
      if($test == false){
          throw new \Exception("The system was deactivated.");
      }
  }
  catch (\Exception $e) {
    $this->flashSession->error($e->getMessage());
  }
}


26.2k
Accepted
answer
edited May '16

Hi all, I have solved, below is a guide. Thanks @Aleksandre Vardanidze and @mraspor

1 You need throw one error


public function testAction($id){
        throw new \Exception("Thanhansoft");
    }

2 Config dispatcher in services.php


$di->set('dispatcher', function () {
    // Create an EventsManager
    $eventsManager = new EventsManager();

    // Attach a listener
    $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
        // Handle 404 exceptions
        if ($exception instanceof DispatchException) {
            $dispatcher->forward(
                array(
                    'controller' => 'index',
                    'action' => 'show404'
                )
            );
            return false;
        }

        // Alternative way, controller or action doesn't exist
        switch ($exception->getCode()) {
            case Dispatcher::EXCEPTION_NO_DI:
                if ($dispatcher->getControllerName() == 'admin') {
                    $msg = $exception->getMessage();
                    $dispatcher->forward([
                        'namespace' => 'App\Modules\Admin\Controllers',
                        'module' => 'admin',
                        'controller' => 'admin',
                        'action' => 'error',
                        'params' => ['msg' => $msg]
                    ]);
                }
                return false;
            case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
            case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                $dispatcher->forward(
                    array(
                        'controller' => 'index',
                        'action' => 'show404'
                    )
                );
                return false;
        }
    });

    $dispatcher = new MvcDispatcher();
    // Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});

3 And then


<?php
namespace App\Modules\Admin\Controllers;

class AdminController extends \App\Controllers\ControllerBackend {

    public function errorAction($msg = null) {
        echo $msg;
    }
}

This is result: https://s1092.photobucket.com/user/thanhansoft/media/2016-05-12230524zpsmffucmwf.png.html