yii2 url not found shown while accessing controller action

∥☆過路亽.° 提交于 2020-01-03 15:55:08

问题


I have created a controller function like

public function actionRateTicket($id){

}

The urlManager has the following configuration

'urlManager' => [
            'class' => 'yii\web\UrlManager',
            // Disable index.php
            'showScriptName' => false,
            // Disable r= routes
            'enablePrettyUrl' => true,'enableStrictParsing' => true,

            'rules' => array(
                    '<controller:\w+>/<id:\d+>'              => '<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                    '<controller:\w+>/<action:\w+>'          => '<controller>/<action>',
            ),
        ],

But when I tried to access http://mydomainname/web/ticket/rate-ticket/2333 , (ticket is the name of controller(TicketController.php)) it is showing Not Found (#404) error.

What is the problem here? I am able to access all controller actions with a single camel case characters like actionView, actionEdit etc, but actionRateTicket, I am not able to acess. If the actionRateTicket is renamed to actionRate, it is working.

My controller is like this

<?php

namespace app\controllers;

use Yii;
use app\models\Techs;
use app\models\Ticket;
use app\models\Assignment;
use app\models\TicketSearch;
use app\models\ComplainType;
use app\models\TicketComplain;
use app\models\User;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Adusers;
use yii\web\UploadedFile;
use app\helpers\Utils;
use yii\db\Expression;
use yii\filters\AccessControl;


/**
 * TicketController implements the CRUD actions for Ticket model.
 */
class TicketController extends Controller {
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only'  => [
                    'index', 
                    'view',
                    'update',
                    'delete', 
                    'newticket'
                ],
                'rules' => [
                    [
                        'actions' => [
                            'index',
                            'view', 
                            'update',
                            'delete', 
                            'newticket'
                        ],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    public function actionRateTicket($id) {
        echo "in"; echo $id;
        exit;
    }
}
?>

My .htaccess of web folder is

RewriteEngine on

# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

回答1:


Change the action name for actionRateticket, and the call should be http://mydomainname/web/ticket/rateticket/2333 with rateticket.




回答2:


try using the following line:

use yii\helpers\Url;



回答3:


you need to specify the action in access behavior

'access' => [
                    'class' => AccessControl::className(),
                    'only' => ['rate-ticket', 'index','view','update','delete','newticket' ],
                    'rules' => [
                            [
                                    'actions' => ['rate-ticket', 'index','view','update','delete','newticket', ],
                                    'allow' => true,
                                    'roles' => ['@'],
                            ],

                    ],
            ],



回答4:


I believe it is because of the weird way that Yii uses dashes instead of camel case. Try changing your rules from <action:\w+> to <action:[-\w]+> since the dash is not a word character!

I can't find the default rules to verify that they also check for the dash but that should work.




回答5:


yii2 uses more SEO friendly url for camel case controller. All camel case is converted to lowercase with dash like this:

UserProfileController
index.php?r=user-profile


来源:https://stackoverflow.com/questions/30163778/yii2-url-not-found-shown-while-accessing-controller-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!