差(什么是在这两个例子中Laravel功能with()
和compact()
之间的差:什么是与之间()和紧凑)在Laravel
实施例1:
return View::make('books.index')->with('booksList', $booksList);
实施例2:
return View::make('books.index',compact('booksList'));
差(什么是在这两个例子中Laravel功能with()
和compact()
之间的差:什么是与之间()和紧凑)在Laravel
实施例1:
return View::make('books.index')->with('booksList', $booksList);
实施例2:
return View::make('books.index',compact('booksList'));
好compact()
是PHP function是一个变量列表转换成一个关联数组,其中关键字为变量名和值变量的实际值。
实际的问题应该是:是什么确实没有一个
return View::make('books.index')->with('booksList', $booksList);
和
return View::make('books.index', array('booksList' => $booksList));
答案之间的区别。他们都将项目添加到视图数据。
句法上讲,View::make()
而with()
既需要只接受一个数组,两个字符串:
with('booksList', $booksList);
或能可能持有多个变量数组:
with(array('booksList' => $booksList, 'foo' => $bar));
这也意味着,compact()
可与with()
一起使用:
return View::make('books.index')->with(compact($booksList));
谢谢,详细。 – 2015-02-11 20:58:31
不客气:) – lukasgeiter 2015-02-11 21:00:23
紧凑方法pass ES阵列数据以构造其中存储到$data
类属性:
public function __construct(Factory $factory, EngineInterface $engine, $view, $path, $data = array())
{
$this->view = $view;
$this->path = $path;
$this->engine = $engine;
$this->factory = $factory;
$this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data;
}
虽然with()
方法接受一个阵列或字符串,和在情况下,它是它执行array_merge所述参数阵列否则追加数据到一个键作为参数传递给$data
属性,所以如果您使用构造函数,您将被迫通过array
,而with()
接受具有值的单个键。
public function with($key, $value = null)
{
if (is_array($key))
{
$this->data = array_merge($this->data, $key);
}
else
{
$this->data[$key] = $value;
}
return $this;
}
详细http://stackoverflow.com/a/22413121/6521116 – 2017-05-11 06:37:38