简体   繁体   English

Yii2 不能调用操作 Function?

[英]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.我试图在我的 controller 中调用一个操作,但它会导致 404 页面。

I have module called admin and it has controller called Product.我有一个名为 admin 的模块,它有名为 Product 的 controller。 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在我看来,我正在尝试如下调用 url

<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.但它正在抛出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:问题出在你 url 上:

admin/product/purchaselocalnumber admin/product/purchaselocalnumber

your action is named:您的操作名为:

actionPurchaseLocalNumber actionPurchaseLocalNumber

so you url must be like: admin/product/purchase-local-number所以你 url 必须像: admin/product/purchase-local-number

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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