model view controller - yii2: new action is notFound 404 -
i want add new action in controller in yii2 project :
i have error add new action ! new action have error : not found (#404)
<?php namespace soft\controllers; use yii; use soft\models\reserve; use soft\models\reservesearch; use yii\web\controller; use yii\web\notfoundhttpexception; use yii\filters\verbfilter; use \yii\web\response; use yii\helpers\html; /** * reservecontroller implements crud actions reserve model. */ class reservecontroller extends controller { /** * @inheritdoc */ public function behaviors() { return [ 'verbs' => [ 'class' => verbfilter::classname(), 'actions' => [ 'delete' => ['post'], 'bulk-delete' => ['post'], ], ], ]; } /** * lists reserve models. * @return mixed */ public function actionindex() { $searchmodel = new reservesearch(); $dataprovider = $searchmodel->search(yii::$app->request->queryparams); return $this->render('index', [ 'searchmodel' => $searchmodel, 'dataprovider' => $dataprovider, ]); } public function actionjson() { die; } /** * displays single reserve model. * @param string $id * @return mixed */ public function actionview($id) { $request = yii::$app->request; if ($request->isajax) { yii::$app->response->format = response::format_json; return [ 'title' => "reserve #" . $id, 'content' => $this->renderajax('view', [ 'model' => $this->findmodel($id), ]), 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::a('edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote']) ]; } else { return $this->render('view', [ 'model' => $this->findmodel($id), ]); } } /** * creates new reserve model. * ajax request return json object * , non-ajax request if creation successful, browser redirected 'view' page. * @return mixed */ public function actioncreate() { $request = yii::$app->request; $model = new reserve(); if ($request->isajax) { /* * process ajax request */ yii::$app->response->format = response::format_json; if ($request->isget) { return [ 'title' => "create new reserve", 'content' => $this->renderajax('create', [ 'model' => $model, ]), 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::button('save', ['class' => 'btn btn-primary', 'type' => "submit"]) ]; } else if ($model->load($request->post()) && $model->save()) { return [ 'forcereload' => '#crud-datatable-pjax', 'title' => "create new reserve", 'content' => '<span class="text-success">create reserve success</span>', 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::a('create more', ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote']) ]; } else { return [ 'title' => "create new reserve", 'content' => $this->renderajax('create', [ 'model' => $model, ]), 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::button('save', ['class' => 'btn btn-primary', 'type' => "submit"]) ]; } } else { /* * process non-ajax request */ if ($model->load($request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } } } /** * updates existing reserve model. * ajax request return json object * , non-ajax request if update successful, browser redirected 'view' page. * @param string $id * @return mixed */ public function actionupdate($id) { $request = yii::$app->request; $model = $this->findmodel($id); if ($request->isajax) { /* * process ajax request */ yii::$app->response->format = response::format_json; if ($request->isget) { return [ 'title' => "update reserve #" . $id, 'content' => $this->renderajax('update', [ 'model' => $model, ]), 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::button('save', ['class' => 'btn btn-primary', 'type' => "submit"]) ]; } else if ($model->load($request->post()) && $model->save()) { return [ 'forcereload' => '#crud-datatable-pjax', 'title' => "reserve #" . $id, 'content' => $this->renderajax('view', [ 'model' => $model, ]), 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::a('edit', ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote']) ]; } else { return [ 'title' => "update reserve #" . $id, 'content' => $this->renderajax('update', [ 'model' => $model, ]), 'footer' => html::button('close', ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) . html::button('save', ['class' => 'btn btn-primary', 'type' => "submit"]) ]; } } else { /* * process non-ajax request */ if ($model->load($request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } } } /** * delete existing reserve model. * ajax request return json object * , non-ajax request if deletion successful, browser redirected 'index' page. * @param string $id * @return mixed */ public function actiondelete($id) { $request = yii::$app->request; $this->findmodel($id)->delete(); if ($request->isajax) { /* * process ajax request */ yii::$app->response->format = response::format_json; return ['forceclose' => true, 'forcereload' => '#crud-datatable-pjax']; } else { /* * process non-ajax request */ return $this->redirect(['index']); } } /** * delete multiple existing reserve model. * ajax request return json object * , non-ajax request if deletion successful, browser redirected 'index' page. * @param string $id * @return mixed */ public function actionbulkdelete() { $request = yii::$app->request; $pks = explode(',', $request->post('pks')); // array or selected records primary keys foreach ($pks $pk) { $model = $this->findmodel($pk); $model->delete(); } if ($request->isajax) { /* * process ajax request */ yii::$app->response->format = response::format_json; return ['forceclose' => true, 'forcereload' => '#crud-datatable-pjax']; } else { /* * process non-ajax request */ return $this->redirect(['index']); } } /** * finds reserve model based on primary key value. * if model not found, 404 http exception thrown. * @param string $id * @return reserve loaded model * @throws notfoundhttpexception if model cannot found */ protected function findmodel($id) { if (($model = reserve::findone($id)) !== null) { return $model; } else { throw new notfoundhttpexception('the requested page not exist.'); } } }
why action json
not found action create
available ؟!
this working :admin2.localhost/en/reserve/create
not working : admin2.localhost/en/reserve/json
this main.php :
<?php $params = array_merge( require (__dir__ . '/../../common/config/params.php'), require (__dir__ . '/params.php') ); $config = [ 'id' => 'app-soft', 'basepath' => dirname(__dir__), 'controllernamespace' => 'soft\controllers', 'bootstrap' => ['log'], 'modules' => [], 'components' => [ 'user' => [ 'identityclass' => 'main\models\custom\softuser', 'enableautologin' => true, ], 'log' => [ 'tracelevel' => yii_debug ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\filetarget', 'levels' => ['error', 'warning'], ], ], ], 'errorhandler' => [ 'erroraction' => 'site/error', ], 'request' => [ // !!! insert secret key in following (if empty) - required cookie validation 'cookievalidationkey' => 'aaaaaaaaaaaaaaa', ], 'runtime'=>[ 'class'=>'soft\config\runtime', ], 'urlmanager' => [ 'rules' => [ 'package' => 'site/package', ], ], ], 'params' => $params, ]; return $config;
thank guidance.
my params.php have problem , fixed :
'urlrules' => [ '' => 'site/index', 'login/' => 'site/login', 'signup/' => 'site/signup', '<controller:[\w-]+>/<action:\w+>' => '<controller>/<action>', '<controller:[\w-]+>/<id:\d+>' => '<controller>/view', '<controller:[\w-]+>/create' => '<controller>/create', '<controller:[\w-]+>/update/<id:\d+>' => '<controller>/update', '<controller:[\w-]+>/delete/<id:\d+>' => '<controller>/delete', '<controller:[\w-]+>/bulk-delete' => '<controller>/bulk-delete', ]
Comments
Post a Comment