1 回答

TA貢獻1810條經(jīng)驗 獲得超4個贊
需要在 $components 數(shù)組上調(diào)用 Flash 組件,然后正常使用它,獨立于調(diào)用該組件的控制器。就像是:
namespace App\Controller\Component;
use Cake\Controller\Component;
class ValidateValueComponent extends Component
{
? ? public $components = ['Flash'];
? ? public function validateValue($var = null){
? ? ? ? try{
? ? ? ? ? ? if(!$var){
? ? ? ? ? ? ? ? throw new \Exception();
? ? ? ? ? ? }
? ? ? ? } catch(\Exception $e){
? ? ? ? ? ? $action = $this->request->params['action'] === 'index' ? 'home' : $this->request->params['action'];
? ? ? ? ? ? $controller = $action === 'home' ? 'Home' : $this->request->params['controller'];
? ? ? ? ? ? $this->Flash->set(__('Message!'));
? ? ? ? ? ? return $this->_registry->getController()->redirect(['controller' => $controller, 'action' => $action]);
? ? ? ? }
? ? }
}
- 1 回答
- 0 關(guān)注
- 168 瀏覽
添加回答
舉報