thinkphp 分组、页面跳转与ajax

本节课大纲:
一、多应用配置技巧
二、使用分组
三、页面跳转
	$this->success('查询成功',U('User/test'));
	$this->redirect('User/test','',5,'页面正在跳');
四、Ajax技巧

前后台公用公共配置文件:

$ pwd
/cygdrive/c/wamp/www/thinkphp5/Admin/Conf

[email protected] /cygdrive/c/wamp/www/thinkphp5/Admin/Conf
$ ls
config.php

[email protected] /cygdrive/c/wamp/www/thinkphp5/Admin/Conf
$ cat config.php
<?php
$arr=include './config.php';

$arr2=array(

);
return  array_merge($arr,$arr2);

?>

// 当前目录下的config.php,这个当前是指主入口的路径:

$arr=include './config.php';

公用配置文件:
$ pwd
/cygdrive/c/wamp/www/thinkphp5

[email protected] /cygdrive/c/wamp/www/thinkphp5
$ ls -ltr config.php
-rwxrwx---+ 1 Administrators None 393 五月  9 13:14 config.php

[email protected] /cygdrive/c/wamp/www/thinkphp5
$ cat config.php
<?php
return array(
        //'配置项'=>'配置值'
        'TMPL_L_DELIM'=>'<{',   //配置左定界符
        'TMPL_R_DELIM'=>'}>',    //配置右定界符
        'DB_PREFIX'=>'',     //设置表前缀
        'DB_DSN'=>'mysql://root:[email protected]:3306/devops', //DSN方式配置数据库信息
        'SHOW_PAGE_TRACE'=>true,//开启页面Trace
        /* 'URL_ROUTER_ON'=>true,
        'URL_ROUTE_RULES'=>array(
         ':id/:num'=>'Index/index',
         ), */
);
?>
[email protected] /cygdrive/c/wamp/www/thinkphp5

thinkphp 分组机制:

<?php
//1.确定应用名称 Home

define('APP_NAME','App');

//2. 确定应用路径  ./Home 当前目录 index.php的当前目录 前台文件夹

define('APP_PATH','./App/');
//开启调试模式

 define('APP_DEBUG',true);
//4.引入核心文件 include 引入的东西错误 代码继续运行  require 出错立即结束

require './ThinkPHP/ThinkPHP.php';

?>

'APP_GROUP_LIST' => 'Home,Admin', //项目分组设定
'DEFAULT_GROUP'  => 'Home', //默认分组

在同一个应用下,再分不同的应用:

$ pwd
/cygdrive/c/wamp/www/thinkphp6/App/Lib/Action

[email protected] /cygdrive/c/wamp/www/thinkphp6/App/Lib/Action
$ ls
Admin  Home  IndexAction.class.php

整个应用叫app应用:

<?php
//1.确定应用名称 Home

define('APP_NAME','App');

//2. 确定应用路径  ./Home 当前目录 index.php的当前目录 前台文件夹

define('APP_PATH','./App/');
//开启调试模式

 define('APP_DEBUG',true);
//4.引入核心文件 include 引入的东西错误 代码继续运行  require 出错立即结束

require './ThinkPHP/ThinkPHP.php';

?>

推荐使用分应用的方式,而不是分组

分应用情况下的访问方式,多应用配置技巧:

$ pwd
/cygdrive/c/wamp/www/thinkphp5

[email protected] /cygdrive/c/wamp/www/thinkphp5
$ ls
Admin  admin.php  config.php  Home  index.php  ThinkPHP

Home前台应用文件夹:

Admin后台应用文件夹:

http://localhost/thinkphp5/admin.php

http://localhost/thinkphp5/index.php

//页面跳转:

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){
	echo "come in Home!";
	$user=M('user');
	$arr=$user->select();
	dump($arr);
	//分配给前台,表示为list
	$this->assign('list','$arr');
	$this->display();
    }
}

前端页面:
<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>

  <table border='1' width='500'>
  <foreach name='list' item='vo'>

  <tr><td><{$vo.username}></td></tr>

  </foreach>

  </table>
 </body>
</html>

//超链接:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>

  <table border='1' width='500'>
  <foreach name='list' item='vo'>

  <tr><td><a href="__URL__/info?id=<{$vo.id}>"><{$vo.username}></a></td></tr>

  </foreach>

  </table>
 </body>
</html>

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){
	echo "come in Home!";
	$user=M('user');
	$arr=$user->select();
	dump($arr);
	//分配给前台,表示为list
	$this->assign('list',$arr);
	$this->display();
    }

	public function info(){
		$id=$_GET['id'];
		$user=M('user');
		$arr=$user->find($id);
		dump($arr);
		if ($arr){
			$this->success('index');
		}
		else {
			//失败后自动跳转到上一页
			$this->error('查询失败');
		}
		$this->assign('list',$arr);
		$this->display();
	}
}

//redirect 跳转:

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
    public function index(){
	echo "come in Home!";
	$user=M('user');
	$arr=$user->select();
	dump($arr);
	//分配给前台,表示为list
	$this->assign('list',$arr);
	$this->display();
    }

	public function info(){
		$id=$_GET['id'];
		$user=M('user');
		$arr=$user->find(100);
		dump($arr);
		if ($arr){
			$this->success('index');
		}
		else {
			//失败后自动跳转到上一页
			$this->redirect('User/index');
		}
		$this->assign('list',$arr);
		$this->display();
	}
}

跳转到:
http://localhost/thinkphp5/index.php/User/index

User/index 页面

Ajax 技巧:

在框架里面,脚本都是被方法所取代

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src="__PUBLIC__/Js/jquery.js"></script>
  <script>
  $(function(){
  $('button').bind('click',function(){

	$.get('__URL__/getAjax',function(jdata){
	<!--alert (JSON.stringify(data));-->
	if (jdata.status==1){
	alert(jdata.data);
	}
  });
  });

   });

  </script>
 </head>
 <body>
   <div style='height:50px;background:yellow' id='did'></div>
   <button>点击</button>
   <script>
     document.write(new Date());
	 </script>
 </body>
</html>

<?php
class IndexAction extends Action {

	public function index(){
		$this->display();
	}

	public function getAjax(){
		//echo 'aaaaaaa';
		$this->ajaxReturn('这里是数据','信息1',1);
	}

}

时间: 2024-08-02 10:07:09

thinkphp 分组、页面跳转与ajax的相关文章

ThinkPHP页面跳转、Ajax技巧详细介绍(十八)

原文:ThinkPHP页面跳转.Ajax技巧详细介绍(十八) ThinkPHP页面跳转.Ajax技巧详细介绍 一.页面跳转 $this->success('查询成功',U('User/test')); ├─//跳当前模块方法下: ├─ $this->success('查询成功','test'); └─//跳到 指定跳转模块方法下 this->success('查询成功',U('User/test')); $this->error('查询失败,3秒后跳会之前的页面/上一页'); //

登录超时页面跳转和ajax请求时的问题!

今天闲来无事听见后端两个coder探讨如何判断请求是否是ajax请求(通过请求头判断是否是xmlhttprequest 和ie下那个布啦布啦的东西 )了解详情后是为了解决判断登陆超时ajax请求到的是登陆页面的html. 然后我给出了一个设计方式方最然我是一个前端)如下: 1,页面跳转的控制器和form提交的控制继承 基类A 然后 A 有做登陆超时的跳转的处理方法. 2,所有的接口也就是他们说的ajax请求的控制器继承基类B  B里面含有登陆超时时返回状态的功能前端在ajax请求的时候可以做状态

ThinkPHP页面跳转和Ajax数据返回

IndexController.class.php的代码没有变,在Common文件夹中新建一个function.php,代码如下: <?php function getTestData(){ $data = array(); for($i=0;$i<10;$i++){ $data[$i]['name'] = 'user-'.$i; $data[$i]['age'] = rand(18,90); } return $data;} 下面是UserController.class.php的代码:&l

ajax 如何实现页面跳转

老师,您好.jquery的ajax如何实现页面跳转?例如:登陆页面属于用户名和密码后,点击登陆,验证用户名和密码,正确后,跳转到其他页面.能否给个例子. 下面列了五个例子来详细说明,这几个例子的主要功能是:在5秒后,自动跳转到同目录下的hello.html(根据自己需要自行修改)文件.1) html的实现   123456 优点:简单缺点:Struts Tiles中无法使用 2) javascript的实现 123456 // 以下方式直接跳转window.location.href='hell

js页面用定时任务通过AJAX获取后台数据,但是从这个页面跳转到其他页面后,定时任务仍然在定时请求后台

setInterval(function(){//ajax 请求后台数据},1000);这个是A页面的定时器然后我在A页面通过其他请求跳转到其他页面之后后台发现A页面的定时器的那个请求仍然在执行为什么会出现这种情况呢?怎么能让我跳转到其他页面之后定时任务不执行呢? yj327243832 | 浏览 1987 次  2014-08-22 17:26 2014-08-23 11:03 最佳答案 个人感觉如果A页面已经跳转到其他页面,那么那个定时器的请求应该不会再被执行,因为A页面在浏览器中应该已经被

jQuery Mobile学习之grid、等待显示的ajax效果、页面跳转、页面跳转传递参数等(二)

Index.cshtml <!-- Start of second page --> <section data-role="page" id="bar"> <header data-role="header"> <h1>Bar</h1> </header> <!-- /header --> <div role="main" class=

爱上MVC~ajax调用分部视图session超时页面跳转问题

回到目录 这个问题出现了很多年了,都没有解决,问题是这样的,有一个需要授权才可以访问的分部视图,在一个view中使用ajax的方法去调用它,然后更新页面的局部DIV,这时,如果你长时间不操作,session会超时,这是正常的,但在超时时,如果你再调用ajax方法,这时你的页面跳转将会在DIV局部完成,这是我们不希望看到的! 事实上,引起这个问题的原因是MVC内部跳转机制的问题,它只到在当前的action所渲染的view上进行跳转,如果我们希望在当前页面跳,需要将MVC方法改为JS方法,代码如下

使用AJAX实现页面跳转

$.ajax({ type:"POST", url: //你的请求程序页面随便啦 async:false,//同步:意思是当有返回值以后才会进行后面的js程序. data://请求需要发送的处理数据 success:function(msg){ if (msg) {//根据返回值进行跳转 window.location.href = '你的跳转的目标地址'; } } ajax只接受最后返回的值,不会响应跳转请求更改浏览器地址栏地址转向的,你需要用js判断ajax的返回值是否要跳转,然后

ngRoute+ngAnimate与JQM中的页面跳转的区别

1.ngRoute+ngAnimate与jQM中的页面跳转有何异同? 相同点: (1)完整的HTML只需要一个 (2)使用异步AJAX请求获取下一个页面 (3)可以实现转场动画 不同点: (1)ngRoute需要配置路由字典:jQM没有,更加灵活 (2)ngRoute访问路由地址的格式——特殊格式的hash http://xxx/index.html#/main jQM访问页面地址——普通的URL http://xxx/tpl/main.html (3)ngRoute访问的路由页面可以使用F5刷