欢迎各位兄弟 发布技术文章
这里的技术是共享的
class BlogAction extends Action{ public function read($id){ echo 'id='.$id; } public function archive($year='2012',$month='01'){ echo 'year='.$year.'&month='.$month; } }复制代码http://serverName/index.php/Blog/read/id/5http://serverName/index.php/Blog/archive/year/2012/month/03复制代码id=5year=2012&month=03复制代码http://serverName/index.php/Blog/archive/month/03/year/2012复制代码http://serverName/index.php/Blog/read/复制代码参数错误:id复制代码public function read($id=0){ echo 'id='.$id; }复制代码http://serverName/index.php/Blog/read/复制代码id=0复制代码http://serverName/index.php/Blog/archive/复制代码 year=2012&month=01复制代码<?php class CityAction extends Action{ public function _empty($name){ //把所有城市的操作解析到city方法 $this->city($name); } //注意 city方法 是 protected 方法 protected function city($name){ //和$name这个城市相关的处理 echo '当前城市' . $name; } }复制代码http://serverName/index.php/City/beijing/http://serverName/index.php/City/shanghai/http://serverName/index.php/City/shenzhen/复制代码当前城市:beijing 当前城市:shanghai 当前城市:shenzhen复制代码http://serverName/index.php/City/shanghai/复制代码http://serverName/index.php/shanghai/复制代码<?php class EmptyAction extends Action{ public function index(){ //根据当前模块名来判断要执行那个城市的操作 $cityName = MODULE_NAME; $this->city($cityName); } //注意 city方法 本身是 protected 方法 protected function city($name){ //和$name这个城市相关的处理 echo '当前城市' . $name; } }复制代码http://serverName/index.php/beijing/http://serverName/index.php/shanghai/http://serverName/index.php/shenzhen/复制代码当前城市:beijing 当前城市:shanghai 当前城市:shenzhen复制代码class IndexAction extends Action{ //前置操作方法 public function _before_index(){ echo 'before<br/>'; } public function index(){ echo 'index<br/>'; } //后置操作方法 public function _after_index(){ echo 'after<br/>'; } }复制代码http://serverName/index.php复制代码beforeindexafter复制代码$User = M('User'); //实例化User对象$result = $User->add($data); if($result){ //设置成功后跳转页面的地址,默认的返回页面是$_SERVER['HTTP_REFERER'] $this->success('新增成功', '/User/list'); } else { //错误页面的默认跳转页面是返回前一页,通常不需要设置 $this->error('新增失败'); }复制代码//默认错误跳转对应的模板文件 'TMPL_ACTION_ERROR' => THINK_PATH . 'Tpl/dispatch_jump.tpl', //默认成功跳转对应的模板文件 'TMPL_ACTION_SUCCESS' => THINK_PATH . 'Tpl/dispatch_jump.tpl',复制代码//默认错误跳转对应的模板文件 'TMPL_ACTION_ERROR' => 'Public:error', //默认成功跳转对应的模板文件 'TMPL_ACTION_SUCCESS' => 'Public:success',复制代码//重定向到New模块的Category操作$this->redirect('New/category', array('cate_id' => 2), 5, '页面跳转中...');复制代码//重定向到指定的URL地址redirect('/New/category/cate_id/2', 5, '页面跳转中...');复制代码$this->ajaxReturn(返回数据[,返回数据格式]);复制代码$data['status'] = 1;$data['info'] = 'info';$data['data'] = $data;$data['url'] = $url;$this->ajaxReturn($data);复制代码$this->ajaxReturn($data,'XML');复制代码$this->success('发布成功',$url);复制代码$data['info'] = '发布成功';$data['url'] = $url;$data['status'] = 1;$this->ajaxReturn($data);复制代码| REQUEST_METHOD | 当前请求类型 |
| IS_GET | 是否GET请求 |
| IS_POST | 是否POST请求 |
| IS_PUT | 是否PUT请求 |
| IS_DELETE | 是否DELETE请求 |
| IS_AJAX | 是否AJAX请求 |
class UserAction extends Action{ public function update(){ if (IS_POST){ $User = M('User'); $User->create(); $User->save(); $this->success('保存完成'); }else{ $this->error('非法请求'); } } }复制代码http://serverName/User/3.htmlhttp://serverName/User/3.shtmlhttp://serverName/User/3.xmlhttp://serverName/User/3.pdf复制代码'URL_HTML_SUFFIX'=>'html'复制代码http://serverName/User/3.html复制代码'URL_HTML_SUFFIX'=>'html|shtml|xml' // 多个用 | 分割复制代码Action/UserAction //用于用户的业务逻辑控制和调度 Event/UserEvent //用于用户的事件响应操作复制代码 A('User','Event');复制代码