https://example.org/contact/import
都可以正常工作。我不确定我在这里做错了什么。 我的控制器代码是:
create
在这里是路线规则 <?php
namespace backend\controllers;
use Yii;
use backend\models\Contact;
class ContactController extends Controller
{
protected function newmodel()
{
return new Contact();
}
public function actionCreate()
{
$objModel = $this->newmodel();
$objReflectionClass = new \ReflectionClass($objModel);
$objModel->load([$objReflectionClass->getShortName() => Yii::$app->request->get()]);
$strView = 'create';
if (!file_exists(Yii::getAlias('@backend') . '/views/' . strtolower($objReflectionClass->getShortName()) . '/create.php'))
{
// default
$strView = '/default/create';
}
return $this->render($strView, [
'objModel' => $objModel,
]);
}
public function actionImport()
{
die("In import function");
}
}
如果创建函数正常工作,那么我相信导入功能也应该起作用。任何人都可以指导我我做错了什么。
thound,谢谢!
我试图手动我为我的URL添加静态路线规则,但这是行不通的。 我添加了该规则
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
'gii'=>'gii/default/login',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
'debug/<controller>/<action>' => 'debug/<controller>/<action>',
'' => 'site/index',
'forgotpassword' => 'site/forgotpassword',
'<action:(contact|login|logout)>' => 'site/<action>',
'<action:(contact|login|logout)>/*' => 'site/<action>',
'<controller:\w+>/'=>'<controller>/index',
'<controller:\w+>/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/view/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<intId:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
],
update
在这里,我在打开时得到的屏幕截图
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
'gii'=>'gii/default/login',
'gii/<controller:\w+>'=>'gii/<controller>',
'gii/<controller:\w+>/<action:\w+>'=>'gii/<controller>/<action>',
'debug/<controller>/<action>' => 'debug/<controller>/<action>',
'' => 'site/index',
'forgotpassword' => 'site/forgotpassword',
'<action:(contact|login|logout)>' => 'site/<action>',
'<action:(contact|login|logout)>/*' => 'site/<action>',
'<controller:\w+>/'=>'<controller>/index',
'<controller:\w+>/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/view/<intId:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<intId:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
'ContactController/actionImport' => 'contact/import',
],
],
403禁止错误是因为您没有定义“访问”行为。 尝试添加此方法:
https://example.org/contact/import
请参阅YII2.0的权威指南