简体   繁体   中英

Yii2 Cant call the action Function?

I am facing a very odd error here. I am trying to call an action in my controller but it leads to 404 page.

I have module called admin and it has controller called Product. The code is as follows

class ProductController extends Controller
{


public function actionIndex()
{
    $session = Yii::$app->session;
    if($session->has('user'))
    {
        $this->layout="admin-layout";
        $numbers= UserPurchasedNumber::find()->all();
        return $this->render('index',["number"=>$numbers]);
    }
    else
    {
         return $this->redirect(['admin/default/login']);
    }
}

public function actionPurchaseLocalNumber() //Dont get called
{
    $session = Yii::$app->session;
    if($session->has('user'))
    {
        $this->layout="admin-layout";
        return $this->render('addlocalnumber');
    }
    else
    {
         return $this->redirect(['admin/default/login']);
    }
}


}

in my view i am trying to call the url as follows

<div style="text-align: center; padding:15px">
                <a href="<?php echo Yii::$app->urlManager->createUrl('admin/product/purchaselocalnumber'); ?>" class="btn btn-primary">Purchase Numbers</a>
            </div>

But it is throwing 404.

Infact when i change the action name to

Purchase

only and call it from view file it works?

Whats the problem??

the problem is located in you url:

admin/product/purchaselocalnumber

your action is named:

actionPurchaseLocalNumber

so you url must be like: admin/product/purchase-local-number

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