>백엔드 개발 >PHP 튜토리얼 >yii2生成URL出现405怎么回事

yii2生成URL出现405怎么回事

WBOY
WBOY원래의
2016-06-06 20:36:351817검색

在使用Yii时候出现如下情况,
在模板页面使用<a href="<?=%20Url::toRoute('site/logout');?>">退出后台</a>
解析后网页生成的代码的URL地址是 localhost/yii2/backend/index.php?r=site/logout,地址是对的,访问的时候出现如下:
Method Not Allowed (#405) Method Not Allowed. This url can only handle the following request methods: POST. The above error occurred while the Web server was processing your request. Please contact us if you think this is a server error. Thank you.

请问为啥会出现405?哪里配置错了?配置文件里面没有配置路由
通过firebug发现前台的是可以的,而且是POST提交。而通过url生成的访问就不对了?求助

回复内容:

在使用Yii时候出现如下情况,
在模板页面使用<a href="<?=%20Url::toRoute('site/logout');?>">退出后台</a>
解析后网页生成的代码的URL地址是 localhost/yii2/backend/index.php?r=site/logout,地址是对的,访问的时候出现如下:
Method Not Allowed (#405) Method Not Allowed. This url can only handle the following request methods: POST. The above error occurred while the Web server was processing your request. Please contact us if you think this is a server error. Thank you.

请问为啥会出现405?哪里配置错了?配置文件里面没有配置路由
通过firebug发现前台的是可以的,而且是POST提交。而通过url生成的访问就不对了?求助

肯定是配置site/logout这个action的verbs了,提示很明显,这个action只接受通过POST发送的请求!
所以你检查下siteController 下的behaviors 是不是配置了

<code>'verb' => [
    'class' => VerbFilter::className(),
    'actions' => [
        'logout' => ['post'],
    ],
],
</code>

如果允许GET访问的话,那么可以改成:

<code>'verb' => [
    'class' => VerbFilter::className(),
    'actions' => [
        'logout' => ['get'], //当然可以是是 ['get', 'post'],同时支持POST和GET两种方式访问
    ],
],
</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.