熊本污 2016-06-27 08:34:29 11388次浏览 7条评论 7 7 0

在使用form表单提交上面放这个方法 防止报错

public $enableCsrfValidation = false; 

控制器内跳转方法

return $this->redirect('index.php?r=index/lists');

带参数跳转到页面ps:去掉",['date'=>$re1]"即可直接从控制器跳转到视图层页面

return $this->render('u_list',['date'=>$re1]);

超链接跳转首页[原生] ps:如果知道内置写法请尽量不要用原生,以免给大家带来不便

<a href="index.php?r=index/index">首页</a>

超链接写法[内置]

<?php  
use yii\helpers\Html;
use yii\helpers\Url;
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
</head>
<body>
	<?= Html::a('跳转页面', ['index/show']) ?>
	<a href="<?= Url::to(['index/show']) ?>">链接跳转</a>
</body>
</html>

表单提交到index控制器下的logo方法

<form action="index.php?r=index/logo" method="post">

带参数超链接

<a href="index.php?r=zhou/delete&id=<?php echo $v['id']?>">刪除</a>

放到框架里的frontend/config/main.php 设置默认跳转首页 访问到 frontend/web即可

'defaultRoute'=>'index/index',   第九行:return [ 下面   'id' => 'app-frontend',的上面

视图层表单页面引用frontend\web下的jquery.js文件

<script type="text/javascript" src="<?php echo Yii::$app->request->baseUrl?>/jquery.js?>"></script>

原生sql语句使用ps:用内置gii方法或者手写model层才是yii操作数据库的正确方法

$sql = "select * from tables";
$re = Yii::$app()->db->createCommand($sql)->queruAll();

设置session ps: yii2 session的使用方法原文链接:http://www.kuitao8.com/20141213/3351.shtml

public function actionIndex()
 	{
 		//设置session
		$session = Yii::$app->session;
		$session->set('aa', 'aaa');
		//OR
		$session['ss'] = 'sss';
		//OR
		$_SESSION['dd'] = 'ddd';
 		return $this->redirect('index.php?r=index/show');
 	}

数组设置session ps:在控制器内使用session的时候要在上面加上:use yii\web\session;

public function actionIndex()
 	{
 		//数组session设置
 		$session = Yii::$app->session;
		$session['user'] = [
	    'id' => 1,
	    'username' => 'yiiuser',
		];
		return $this->redirect('index.php?r=index/show');		
 	}

读取session

public function actionShow()
 	{
 		// 读取session
		$session = Yii::$app->session;
		$aa = $session->get('aa');
		//OR
		$ss = $session['ss'];
		//OR
		$dd = isset($_SESSION['dd']) ? $_SESSION['dd'] : null;
		echo $aa; echo "<br>"; echo $ss; echo "<br>"; echo $dd;
		die;
		
 	}

数组读取session

public function actionShow()
 	{
 		//数组session读取
 		$session = Yii::$app->session;
 		echo $session['user']['id'];
		echo $session['user']['username'];
		 echo  "<br>";
		$session1 = $session['user.id'] = 1;
		$session2 = $session['user.username'] = 'yiiuser';
		echo $session1;
		echo $session2;
 	}

session销毁

public function actionDestroy()
 	{
 		//销毁session
		$session = Yii::$app->session;
		$session->remove('user_id');
		//OR
		unset($session['user_id']);
		//OR
		unset($_SESSION['user_id']);
 	}

yii2中的gii使用有一位“白狼栈”的大哥总结的很好建议想了解gii的大家去看看

http://www.manks.top/article/yii2_gii_description

觉得很赞
您需要登录后才可以评论。登录 | 立即注册