简体   繁体   中英

Custom action Error in Yii2

I have a controller called TextController,I created a custom action in my controller called trans controller which I used to manage multi languages.The problem arises when I submit the form.It shows me that Not Found (#404) The requested page does not exist.
I confused with this...Can anybody help me????
I am attaching my controller and view here.
My Controller

public function actionTranslate()
{
    // echo 'dddddddddd';die();

     for($x = 0; $x < count($_POST['english']); $x++ )
{
    $model = $this->findModel($_POST['text_id'][$x]);
    $model->trans_text_id = $_POST['text_id'][$x];
    $model->trans_text_label =$_POST['label_id'][$x];
    $model->English =$_POST['english'][$x];
    $model->Spanish =$_POST['spanish'][$x];
    $model->French =$_POST['french'][$x] ;
    $model->German =$_POST['german'][$x] ; 
    // echo '<pre>';print_r($model->trans_text_id);

  $model->save(false);

}

echo Yii::$app->session->setFlash('success', 'Translation Saved');

return $this->redirect(Yii::$app->request->referrer);
}

My Form

   <?php 
   use yii\helpers\Html;
   use app\modules\admin\models\TransLabels;
   use app\modules\admin\models\TransText;

        $ids = $_GET['id'];
        // echo $ids;

        $model = TransLabels::find()->where(['trans_page_title' => $ids])->all();

                     // echo '<pre>';print_r($model);die();

     ?>


<table class="table table-striped table-bordered" >
  <thead>
    <tr>
      <th>Labels</th>
      <th>English</th>
      <th>Spanish</th>
      <th>French</th>
      <th>German</th>
    </tr>
  </thead>
  <tbody>

  <form action="<?php echo Yii::$app->homeUrl;?>admin/text/translate" method="post" >
     <?php
     if (Yii::$app->session->hasFlash('success')) {?>
        <div>
        <center><h1><span style="color:green; text-align:centre "><?php
          print_r(Yii::$app->session->getFlash('success'));
          ?> </span></h1></center>
        </div>
        <?php }
         ?>
  <?php
  foreach($model as $expertn)
    {    
    $title=$expertn['trans_labels'];
    $label_id = $expertn['trans_label_id'];
    $sql = TransText::find()->where(['trans_text_label'=>$label_id])->all();
    $eng = $sql[0]->English;
    $span = $sql[0]->Spanish;
    $fren = $sql[0]->French;
    $ger = $sql[0]->German;
    $text_id = $sql[0]->trans_text_id;
   // echo '<pre>';print_r($ger);
// echo $expertn['trans_label_id'];
        echo '<tr>'; // start a new row   
        echo '<td data-label="Payment">';
        echo $expertn['trans_labels'];
        echo '<input type="hidden" name="text_id[]" value="'.$text_id.'"/>';
        echo '<input type="hidden" name="label_id[]" value="'.$expertn['trans_label_id'].'"/>';
        echo '</td>';
        echo '<td data-label="Issue Date">';
        echo '<input type="text" name="english[]" value="'.$eng.'"/>';
        echo '</td>';
        echo '<td data-label="Issue Date">';
        echo '<input type="text"name="spanish[]" value="'.$span.'"/>';
        echo '</td>';
        echo '<td data-label="Issue Date">';
        echo '<input type="text"name="french[]" value="'.$fren.'"/>';
        echo '</td>';
        echo '<td data-label="Issue Date">';
        echo '<input type="text"name="german[]" value="'.$ger.'"/>';
        echo '</td>';

        // echo '<td>';
        // echo $expertn['tcount'];
        // echo '</td>';
        echo '</tr>';

    }
        echo '<input type="submit"  name="submit" id="sub" value="Save" class="btn btn-primary bton"/>';
    ?>
</form>
  </tbody>
</table>  

Thanks in advance...

If you are having controller named TextController then in view file

...
<form action="<?php echo Yii::$app->request/baseUrl;?>admin/text/translate" method="post" >
...

OR

...
<form action="<?php echo Yii::$app->homeUrl;?>/admin/text/translate" method="post" >
...

and in TextController.php

public function actionTranslate()
{
    // echo 'dddddddddd';die();

    for($x = 0; $x < count($_POST['english']); $x++ )
    {
        if($model = $this->findModel($_POST['text_id'][$x]))
        {
            $model->trans_text_id = $_POST['text_id'][$x];
            $model->trans_text_label =$_POST['label_id'][$x];
            $model->English =$_POST['english'][$x];
            $model->Spanish =$_POST['spanish'][$x];
            $model->French =$_POST['french'][$x] ;
            $model->German =$_POST['german'][$x] ; 
            // echo '<pre>';print_r($model->trans_text_id);

            $model->save(false);
            unset($model);            
        }
    }

    echo Yii::$app->session->setFlash('success', 'Translation Saved');

    return $this->redirect(Yii::$app->request->referrer);
}

You should put it if condition (because if not found in db then?) in for loop and unset at the end of if condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM