[Day 10] Response

单纯使用return是最简单最基本的回传方式

public function index(Request $request)
    {
        return 'fruit'; 
    }

https://ithelp.ithome.com.tw/upload/images/20210925/20142046tjxYNWZfnq.png


return response():是一个快速可以使用的函数,可以用来包装要传递的资料

public function index(Request $request)
    {
        return response('fruit'); 
    }

也可以得到相同的结果
https://ithelp.ithome.com.tw/upload/images/20210925/20142046tjxYNWZfnq.png

使用response()的好处

当route/web.php中写

Route::get('/products', function () {
    return response('Hello World', 200) //response(内容, 回传状态)
                  ->header('Content-Type', 'text/plain');
});

当app/HTTP/Controllers/ProductController.php中写

public function index(Request $request)
    {

        return response()->header;
    }

就可以在路径/products看到
https://ithelp.ithome.com.tw/upload/images/20210925/20142046wD9KF49bQ7.png

response('Hello World', 200) :回传资料时後面还能再带一个status 参数,常见的有200、400、500,其代表的意义是回传资料的状态

  • 200告诉使用者与server互动的行为是正常可以使用的
  • 400告诉使用者资料有误
  • 500告诉使用者服务器异常

return redirect():让使用者导页

当route/web.php中写

Route::get('/',function(){
    return view('welcome');
});

当app/HTTP/Controllers/ProductController.php中写

public function index(Request $request)
    {

        return redirect('/');
    }

就会让使用者进入/products时就导向主页
https://ithelp.ithome.com.tw/upload/images/20210925/20142046ZkgXNKuAaq.png


<<:  Day20:Flow 想在其他的执行绪执行,可以吗?

>>:  Vue.js 从零开始:v-model

Day 26 - Stripe Follow Along Dropdown Navigation [更新]

前言 JS 30 是由加拿大的全端工程师 Wes Bos 免费提供的 JavaScript 简单应用...

Day5:def函数

Python定义函式有几种方法 Class函式 def函式 我们今天先介绍def函式。 先来解释函式...

DAY26 MongoDB 汇入与汇出资料

DAY26 MongoDB 汇入与汇出资料 系统运作时常发生在特定环境才会出错的问题,其他环境又没发...

【Day 3】Google Apps Script - 建立 Apps Script 的三种方式

建立 Apps Script 的三种方式 。 今日要点: 》建立 Apps Script 专案 》...

【第三十天 - Flutter 结赛感想、期许、愿景】

感想 在这三十天,我整理了一下 Sample Code,虽然对初学者来说,可能偏难,但是这些过程,是...