Yii2 backend url manager rules for module -
i have created module in yii2 using gii code generator. new generated module location is
backend/modules/cms
cms new generated module name.
backend/config/main.php after setting module configuration looks following
return [ 'id' => 'app-backend', 'basepath' => dirname(__dir__), 'controllernamespace' => 'backend\controllers', 'bootstrap' => ['log'], 'modules' => [ 'gii' => [ 'class' => 'yii\gii\module', //adding gii module 'allowedips' => ['127.0.0.1', '::1'] //allowing ip's ], 'cms' => [ 'class' => 'backend\modules\cms\cms', ], ], 'components' => [ 'user' => [ 'identityclass' => 'common\models\user', 'enableautologin' => true, ], 'log' => [ 'tracelevel' => yii_debug ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\filetarget', 'levels' => ['error', 'warning'], ], ], ], 'errorhandler' => [ 'erroraction' => 'site/error', ], 'urlmanager' => [ 'class' => 'yii\web\urlmanager', 'enableprettyurl' => true, 'showscriptname' => false, 'rules' => [ '<controller:\w+>/<action:\w+>/<id:\w+>' => '<controller>/<action>', '<controller:\w+>/<action:\w+>' => '<controller>/<action>', '<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>', '<module:\w+><controller:\w+>/<action:update|delete>/<id:\d+>' => '<module>/<controller>/<action>', ] ], 'assetmanager' => [ 'bundles' => [ 'yii\web\jqueryasset' => [ 'js' => [] ], ], ], ], 'params' => $params, ];
behaviors function in controller looks after setting access rules
public function behaviors() { return [ 'access' => [ 'class' => accesscontrol::classname(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index', 'create', 'update', 'delete'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => verbfilter::classname(), 'actions' => [ 'delete' => ['post'], ], ], ]; }
only index action of controller accessible. when access other action in controller url changes shows index action contents.
how can access actions in controller?
it appreciable if me resolving issue. my controller
namespace backend\modules\cms\controllers; use yii; use yii\filters\accesscontrol; use backend\modules\cms\models\pages; use backend\modules\cms\models\pagessearch; use yii\web\controller; use yii\web\notfoundhttpexception; use yii\filters\verbfilter; /** * pagescontroller implements crud actions pages model. */ class pagescontroller extends controller { public function behaviors() { return [ 'access' => [ 'class' => accesscontrol::classname(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index', 'create', 'update', 'delete'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => verbfilter::classname(), 'actions' => [ // 'delete' => ['post'], ], ], ]; } /** * lists pages models. * @return mixed */ public function actionindex() { $searchmodel = new pagessearch(); $dataprovider = $searchmodel->search(yii::$app->request->post()); return $this->render('index', [ 'searchmodel' => $searchmodel, 'dataprovider' => $dataprovider, ]); } /** * displays single pages model. * @param integer $id * @return mixed */ public function actionview($id) { return $this->render('view', [ 'model' => $this->findmodel($id), ]); } /** * creates new pages model. * if creation successful, browser redirected 'view' page. * @return mixed */ public function actioncreate() { echo "here"; exit; $model = new pages(); if ($model->load(yii::$app->request->post()) && $model->save()) { return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * updates existing pages model. * if update successful, browser redirected 'view' page. * @param integer $id * @return mixed */ public function actionupdate($id) { $model = $this->findmodel($id); if ($model->load(yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', [ 'model' => $model, ]); } } /** * deletes existing pages model. * if deletion successful, browser redirected 'index' page. * @param integer $id * @return mixed */ public function actiondelete($id) { $this->findmodel($id)->delete(); return $this->redirect(['index']); } /** * finds pages model based on primary key value. * if model not found, 404 http exception thrown. * @param integer $id * @return pages loaded model * @throws notfoundhttpexception if model cannot found */ protected function findmodel($id) { if (($model = pages::findone($id)) !== null) { return $model; } else { throw new notfoundhttpexception('the requested page not exist.'); } } }
i using url follows: http://localhost/yii2-demo/backend/cms/pages/index
what url(s) using? module should be: ?r=cms/pages/index, ?r=cms/pages/create etc...
Comments
Post a Comment