我目前正在研究一个新的大型应用程序,我正在努力确保从一开始就实施最佳实践。我是Laravel的新手,所以我有一个问题是我如何限制整个应用程序的重复。如何防止我的Laravel应用程序重复?
作为一个例子,我现在有一个管理单元,将视图像这样一个资源控制器:
public function index()
{
// $data will be passed to the view
$data = array(
'pageTitle' => 'Manage Products',
'btn' => array(
'title' => 'Add product',
'url' => 'admin.catalog.product.create',
)
);
return View::make('catalog::product.index', $data)->with('products', Product::all());
}
我的观点文件看起来很喜欢这样:
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>SKU</th>
<th>Price</th>
<th>Qty</th>
<th>Created</th>
<th><i class="icon-cog"></i></th>
</tr>
</thead>
<tbody>
@foreach ($products as $product)
<tr>
<td>{{ $product->pid }}</td>
<td><a href="{{ URL::route('admin.catalog.product.edit', $product->pid) }}">{{ $product->name }}</a></td>
<td>{{ $product->sku }}</td>
<td>{{ Currency::display($product->price) }}</td>
<td>{{ $product->quantity }}</td>
<td>{{ Dates::showTimeAgo($product->created_at) }}</td>
<td>
<a href="{{ URL::route('admin.catalog.product.edit', $product->pid) }}" class="btn btn-success btn-mini pull-left">Edit</a>
{{ Form::open(array('route' => array('admin.catalog.product.destroy', $product->pid), 'method' => 'delete', 'data-confirm' => 'Are you sure?')) }}
<button type="submit" href="{{ URL::route('admin.catalog.product.destroy', $product->pid) }}" class="btn btn-danger btn-mini">Delete</button>
{{ Form::close() }}
</td>
</tr>
@endforeach
</tbody>
是有一种方法可以对所有管理表使用一个视图文件,并通过控制器传递我想要的标题和正文所需的列,而不是在视图中对其进行硬编码在上面?
我知道我可以通过$ data数组,但不知道如何去通过我想通过这个列传递。任何想法或建议将不胜感激。
你是对的,这更是一个PHP的问题。我感谢您的反馈,我会给你的建议去看看它是如何工作的。谢谢 – Robert