2016-07-11 6 views
0

i eine neue Aktion in meinem Controller in yii2 Projekt hinzufügen mag:yii2: neue Aktion ist NOTFOUND 404

i Fehler haben für jede neue Aktion hinzuzufügen! neue Aktion haben diesen Fehler: 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 the CRUD actions for Reserve model. 
*/ 
class ReserveController extends Controller { 

    /** 
    * @inheritdoc 
    */ 
    public function behaviors() { 
     return [ 
      'verbs' => [ 
       'class' => VerbFilter::className(), 
       'actions' => [ 
        'delete' => ['post'], 
        'bulk-delete' => ['post'], 
       ], 
      ], 
     ]; 
    } 

    /** 
    * Lists all 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 a 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 a new Reserve model. 
    * For ajax request will return json object 
    * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page. 
    * @return mixed 
    */ 
    public function actionCreate() { 
     $request = Yii::$app->request; 
     $model = new Reserve(); 

     if ($request->isAjax) { 
      /* 
      * Process for 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 for 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 an existing Reserve model. 
    * For ajax request will return json object 
    * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page. 
    * @param string $id 
    * @return mixed 
    */ 
    public function actionUpdate($id) { 
     $request = Yii::$app->request; 
     $model = $this->findModel($id); 

     if ($request->isAjax) { 
      /* 
      * Process for 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 for 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 an existing Reserve model. 
    * For ajax request will return json object 
    * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page. 
    * @param string $id 
    * @return mixed 
    */ 
    public function actionDelete($id) { 
     $request = Yii::$app->request; 
     $this->findModel($id)->delete(); 

     if ($request->isAjax) { 
      /* 
      * Process for ajax request 
      */ 
      Yii::$app->response->format = Response::FORMAT_JSON; 
      return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax']; 
     } else { 
      /* 
      * Process for non-ajax request 
      */ 
      return $this->redirect(['index']); 
     } 
    } 

    /** 
    * Delete multiple existing Reserve model. 
    * For ajax request will return json object 
    * and for non-ajax request if deletion is successful, the browser will be redirected to the '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 as $pk) { 
      $model = $this->findModel($pk); 
      $model->delete(); 
     } 

     if ($request->isAjax) { 
      /* 
      * Process for ajax request 
      */ 
      Yii::$app->response->format = Response::FORMAT_JSON; 
      return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax']; 
     } else { 
      /* 
      * Process for non-ajax request 
      */ 
      return $this->redirect(['index']); 
     } 
    } 

    /** 
    * Finds the Reserve model based on its primary key value. 
    * If the model is not found, a 404 HTTP exception will be thrown. 
    * @param string $id 
    * @return Reserve the loaded model 
    * @throws NotFoundHttpException if the model cannot be found 
    */ 
    protected function findModel($id) { 
     if (($model = Reserve::findOne($id)) !== null) { 
      return $model; 
     } else { 
      throw new NotFoundHttpException('The requested page does not exist.'); 
     } 
    } 

} 

warum Aktion json ist nicht gefunden, aber Aktion create ist verfügbar?!

dies funktioniert: admin2.localhost/en/reserve/create aber das funktioniert nicht: admin2.localhost/en/reserve/json

das ist mein 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 a secret key in the following (if it is empty) - this is required by cookie validation 
      'cookieValidationKey' => 'aaaaaaaaaaaaaaa', 
     ], 
     'runtime'=>[ 
      'class'=>'soft\config\Runtime', 
     ], 
     'urlManager' => [ 
      'rules' => [ 
       'package' => 'site/package', 
      ], 
     ], 
    ], 
    'params' => $params, 
]; 

return $config; 

Danke für Ihre Unterstützung.

+0

Sieht aus wie actionJson() außerhalb der Klasse definieren. –

+0

Dieser Fehler ist nur in meinem Beitrag! – Saltern

+0

Immer noch Fehler in Ihrem Code. Jetzt actionjson-Methode in actioncreate(). Können Sie bitte den richtigen Code angeben? –

Antwort

0

mein params.php Problem haben und behoben:

'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', 
    ] 
+0

gelöst! حل شش :-) – Saltern

+0

Was ist der Grund dafür? – yafater

+1

Diese Zeile war nicht in meinem Code: '/' => '/', – Saltern