如何在Yii2中启用干净的URL.我想删除index.php和'?' 来自url参数.需要在Yii2中编辑哪个部分?
有没有办法重定向到Yii 2中的行为方法登录以外的页面?
我的行为方法内容:
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
'only' => [ 'create','update' ],
'rules' => [
[
'allow' => true,
'actions' => [ 'create'],
'roles' => ['@'],
],
[
'allow' => true,
'actions' => ['logout'],
'roles' => ['?'],
],
],
],
];
}
Run Code Online (Sandbox Code Playgroud)
但它重定向到登录.我需要指定另一个重定向页面或调用:
throw new \yii\web\HttpException(403, 'The requested Item could not be found.');
Run Code Online (Sandbox Code Playgroud) 我正在尝试在Yii 2中创建一个辅助函数.在Yii 2中,要添加哪个文件夹来创建自定义帮助函数,以及如何在控制器中使用它?
每当我尝试使用composer "composer require packagename/package"或者使用composer添加新包时
"composer.phar update",我都会更新所有已安装的包.作曲家有没有选择排除一些我不需要更新的包?
我想通过生成中提到的方法中的超链接 http://www.yiiframework.com/doc-2.0/guide-helper-html.html#hyperlinks这样
Html::a('<b>Register</b>',
['story/create', array('id' =>39,'usr'=>'11')],
['class' => 'profile-link'])
Run Code Online (Sandbox Code Playgroud)
我想得到网址 story/create/id/39/usr/11
但它正在产生
story/create?1%5Bid%5D=39&1%5Busr%5D=1
Run Code Online (Sandbox Code Playgroud)
我启用了yii2的干净网址功能
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
], also.
Run Code Online (Sandbox Code Playgroud)
如何实现这一目标?
我有以下规则和方案
public function rules(){
return [
[['name','email','password'],'required'],
['email','myvalidation'],
['email','email'],
[['name', 'email', 'password'], 'required', 'on' => 'register'],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['login'] = ['name','password','email'];//Scenario Values Only Accepted
return $scenarios;
}
Run Code Online (Sandbox Code Playgroud)
我希望rule 'myvalidation'仅应用于login场景而在其他情况下根本不应用.如何实现这一点Yii2?
我试图通过使用关系通过Kartik GridView小部件列出一些数据yii2.我有这些表
staffs
CREATE TABLE IF NOT EXISTS `staffs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
`department_id` int(11) DEFAULT NULL,
`designation_id` int(11) DEFAULT NULL,
`username` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`emailid` varchar(250) CHARACTER SET utf8 DEFAULT NULL,
`staffrights` tinyint(2) DEFAULT '0',
`staffstatus` tinyint(2) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
designations
CREATE TABLE IF NOT EXISTS `designations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`designname` varchar(150) NOT NULL, …Run Code Online (Sandbox Code Playgroud) 没有使用数据库的任何想法?我已经完成了基本模板.在那个app/models/User.php中,他们提供了基于的验证
private static $users = [
'100' => [
'id' => '100',
'username' => 'admin',
'password' => 'admin',
'authKey' => 'test100key',
'accessToken' => '100-token',
],
'101' => [
'id' => '101',
'username' => 'demo',
'password' => 'demo',
'authKey' => 'test101key',
'accessToken' => '101-token',
],
];
Run Code Online (Sandbox Code Playgroud)
但是我希望基于不同的条件进行登录验证,例如,如果变量满足某些条件,他应该登录.或者我是否可以Identity手动设置任何想法?
我提到了http://www.yiiframework.com/doc-2.0/yii-web-identityinterface.html,它还提到了仅使用数据库.有必要设置'authKey'和 'accessToken'?
或者任何人都可以解释Yii 2.0中的身份验证流程,哪个值要设置,顺序是什么?
我试图在yii2空闲一段时间后自动注销用户.在web.php我补充说
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'authTimeout'=>100
],
Run Code Online (Sandbox Code Playgroud)
在里面components.我正在使用基本模板.但它没有自动退出.这在Yii2中有用吗?我正在关注http://www.yiiframework.com/doc-2.0/yii-web-user.html上的文档
基于https://github.com/wbraganca/yii2-dynamicform/wiki/Dynamic-Forms-With-Yii2-relation-trait-(VERY-EASY),我正在尝试实现动态表单.Create工作正常,但是在Update窗体中,如果我删除了任何动态表单元素,它就不会被删除,但如果我添加了Update操作,它就会被保存.这是我的更新代码
public function actionUpdate($id)
{
$modelAlumni = $this->findModel($id);
$modelsJob = $modelAlumni->jobs;
if ($modelAlumni->loadAll(Yii::$app->request->post()) && $modelAlumni->saveAll()) {
return $this->redirect(['view', 'id' => $modelAlumni->id]);
} else {
return $this->render('update', [
'modelAlumni' => $modelAlumni,
'modelsJob' => (empty($modelsJob)) ? [new Job] : $modelsJob
]);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不删除?
这是我的校友模特
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "alumni".
*
* @property integer $id
* @property string $name
* @property integer $gender
* @property integer $contact_number
* @property string …Run Code Online (Sandbox Code Playgroud)