Home Backend Development PHP Tutorial Laravel响应和视图

Laravel响应和视图

Jun 23, 2016 pm 01:27 PM


基础响应

    //直接响应字符串
    Route::get('example/test20', function(){
        return 'hello world';
    });
    //定制HTTP响应
    Route::get('example/test21', function(){
        return Response::make('内容不存在', 404);
    });
    //响应视图
    Route::get('example/test22', function(){
        return Response::view('test22');
    });
    //给响应添加cookie
    Route::get('example/test23', function(){
        return Response::view('test22')->withCookie(Cookie::make('key', 'this is value'));

响应重定向

    //响应重定向
    Route::get('example/test24', function(){
        return Redirect::to('example/test21')->with('username', 'xiaoming');
    });
    //带上数据的重定向
    Route::get('example/test25', function(){
        //with 方法将数据写到了Session中,通过Session::get 方法即可获取该数据。
        return Redirect::to('example/test21')->with('username', 'xiaoming');
    });
    //重定向至命名路由
    return Redirect::route('login');
    //重定向值带有命名参数的命名路由
    return Redirect::route('profile', array('user' => 1));
    //重定向至指定的控制器方法
    return Redirect::action('HomeController@index');
    //重定向至指定的控制器方法,并可带上参数
    return Redirect::action('UserController@profile', array('user' => 1));

响应视图

    //响应视图并传递参数
    Route::get('example/test30', function(){
        //第一种方式
        return View::make('test30', array('name' => 'xiaoming'));
        //第二种方式
        //return View::make('test30')->with('name', 'xiaoming2');
        //第三种方式
        //return View::make('test30')->withName('xiaoming3');
        //第四种方式,注:在所有视图中共享同一数据
        //View::share('name', 'Steve');
    });
    //在视图中传入子视图
    Route::get('example/test31', function(){
        $data = array('name' => 'john');
        //子视图放在app/views/child/child_view.php, 你也可以向其传递变量
        return View::make('test30')->with($data)->nest('child', 'child.child_view', $data);
    });

视图组件或视图合成器

如果你希望视图被创建时,就绑上指定数据,可以定义视图组件:

    View::composer('profile', function($view)
    {
        $view->with('count', User::count());
    });

给视图组件添加多视图:

    View::composer(array('profile','dashboard'), function($view)
    {
        $view->with('count', User::count());
    });

如果你使用基于类的视图组件:

    View::composer('profile', 'ProfileComposer');

视图组件类这样创建:

    class ProfileComposer {
        public function compose($view)
        {
            $view->with('count', User::count());
        }
    }

特殊响应

    //创建JSON响应
    return Response::json(array('name' => 'Steve', 'state' => 'CA'));
    //创建JSONP响应
    return Response::json(array('name' => 'Steve', 'state' => 'CA'))->setCallback(Input::get('callback'));
    //文件下载响应
    return Response::download($pathToFile);
    return Response::download($pathToFile, $name, $headers);
    注意:管理文件下载的类库Symfony HttpFoundation要求文件名是ASCII编码的。

响应宏,使用Response::macro自定义响应

    Response::macro('caps', function($value)
    {
        return Response::make(strtoupper($value));
    });

macro 方法接受两个参数,一个指定宏名称和一个闭包。当通过 Response 类调用该名称的宏时,闭包就会被执行:

    return Response::caps('foo');

你可以在 app/start 目录里的文件中定义宏。或者,你也可以通过一个单独的文件组织你的宏,并将该文件包含至某个 start 文件中。

转载于http://www.phpddt.com/php/laravel-response.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

Announcement of 2025 PHP Situation Survey

See all articles