php controller view,Laravel 控制器 Controller 传值到 视图 View 的几种方法总结

从接触编程,到PHP,总共学习了三个月,刚接触laravel框架没多久

对于新人来说,感觉传值有的时候看起来太迷惑。所以在这里整理一下,水平有限,也是第一次用markdown写文档有任何错误或者需要改进的地方请诸位悉心赐教。

总体内容分为传值类型和方法,大概总体感觉如下。

传值类型:一个值,多个值,数组。

方法: with,view,compact

默认视图 test文件下index.blade.php

单个值的传递

with

public function index() {

$test = "测试";

return view('test.index')->with('test',$test);

}

view

public function index() {

return view('test.index', ['test' => '测试']);

}

compact

public function index() {

$test = "测试";

return view('test.index',compact('test'));

}

多个值的传递

with

public function index() {

return view('test.index')->with(["test1" => "测试1", "test2" => "测试2", "test3" => "测试3"]);

}

view

public function index() {

return view('test.index', ['test1' => '测试1','test2' => '测试2','test3' => '测试3']);

}

compact

public function index() {

$test_1 = "测试1";

$test_2 = "测试2";

$test_2 = "测试3";

return view('test.index',compact('test_1','test_2' ,'test_3' ));

}

数组的传递

with

public function index() {

$data = array( 'test1' => '测试1', 'test2' => '测试2', 'test3' => '测试3' );

return view('test.index')->with($data);

}

view

public function index() {

$data["test1"] = "测试1";

$data["test2"] = "测试2";

$data["test3"] = "测试3";

return view('test.index',$data);

}

compact

//推荐此种方法

public function index() {

$test_array = ["测试1","测试2", "测试2"];

return view('test.index',compact('test_array'));

}

以上就是最近整理出来的一些传值方法,不知道写法上还有什么更聪明的写法。

第一次写,主要是以学习的态度。望赐教。

本作品采用《CC 协议》,转载必须注明作者和本文链接

⬇︎第一次零基础搭建的个人博客。欢迎批评指正,大力鞭策!❤︎

旺财的个人博客(⌯¤̴̶̷̀ω¤̴̶̷́)✧

January 17th, 2020

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值