phalcon学习 - 重定向,URL

来源:互联网 发布:游戏编程模式 编辑:程序博客网 时间:2024/05/21 09:05

1 URL重定向:

                  重定向用来在当前的处理中跳转到其它的处理流:

<?php// 此路由重定向到其它的路由$app->post('/old/welcome', function () use ($app) {    $app->response->redirect("new/welcome")->sendHeaders();});$app->post('/new/welcome', function () use ($app) {    echo 'This is the new Welcome';});

//设置一个内部跳转$this->response->redirect( 'posts/index' );// 外部跳转url$this->response->redirect( 'http://blog.csdn.net/q718330882', true );// 设置跳转 http状态$this->resopnse->redirect( 'http://csd.net' , true , 301 );

___重定向不会禁用视图组件。因此,如果你想从一个controller/action重定向到另一个controller/acton上,视图将正常显示。当然,你也可以使用 $this->view->disable() 禁用视图输出。

2 存储/获取 Session数据

$this->session->set("session_name", "session_value");
$this->session->has("session-name");
$this->session->get("session-name");
$this->session->remove("session-name");
$this->session->destroy();

3 数据库操作方法

    添加  直接设置传递过来的值即可 或可以使用save()方法

    更新  save()

    删除  delete()

    查找  find() findFirst()

    保存  save()

4 From 表单接收

    eg:$name= $this->request->getPost("name", "string");

      $email =$this->request->getPost("email", "email");

从容器中获取的服务的最简单方式就是只用get方法,它将从容器中返回一个新的实例:

<?php $request = $di->get( 'request' ); ?>

或者通过下面这种魔术方法的形式调用:

<?php $request = $di->getRequest(); ?>

5 控制器跳转

在控制器中做跳转操作,一般会用到 dispatcher的 forward方法

        $this->dispatcher->forward(array(
            "controller" => "users",
            "action" => "signin"
        ));

也可以定义beaseController 自定义方法进行控制器的跳转。

6  处理Not-Found

当用户访问未定义的路由时, 微应用会试着执行 "Not-Found"处理器。

<?php$app->notFound(function () use ($app) {    $app->response->setStatusCode(404, "Not Found")->sendHeaders();    echo 'This is crazy, but this page was not found!';});


3 0
原创粉丝点击