2013-11-26 29 views
1

我目前正在研究一个新的大型应用程序,我正在努力确保从一开始就实施最佳实践。我是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数组,但不知道如何去通过我想通过这个列传递。任何想法或建议将不胜感激。

回答

3

我不认为这真的是关于Laravel的问题,但是,嘿,我们走了。

您可以将函数添加到模型中,以将列名映射到您的值。

public function getColumns() 
{ 
    return array(
     'ID' => 'pid', 
     'Name' => 'name', 
     .... 
    ); 
} 

public function getColumnData() 
{ 
    $columns = $this->getColumns(); 

    $records = []; 
    foreach ($columns as $column) { 
     if ('name' == $column) { 
      $data = '<a href="' . URL::route('admin.catalog.product.edit', $this->pid) . '">' . $product->name . '</a>'; 
     } if else (..) { 
      ... 
     } else { 
      $data = $this->{$column}; 
     } 
    } 

    return $records; 
} 

而且你的观点将成为这样的事情:

<thead> 
    <tr> 
     @foreach ($records->first()->getColumns() as $column) 
     <th>{{ $column->name }}</th> 
     @endforeach 
    </tr> 
</thead> 
<tbody> 
    @foreach ($records->all() as $record) 
    <tr> 
     @foreach ($record->getColumnData() as $data) 
     <td>{{ $data }}</td> 
     @endforeach 
    </tr> 
    @endforeach 
</tbody> 
+0

你是对的,这更是一个PHP的问题。我感谢您的反馈,我会给你的建议去看看它是如何工作的。谢谢 – Robert

1

我想这

查看:

<table> 
    <thead> 
     <tr> 
      @foreach ($records[0] as $k => $v) 
       <th>{{ ucwords(str_replace('_', ' ', $k)) }}</th> 
      @endforeach 
     </tr> 
    </thead> 
    <tbody> 
     @foreach ($records as $record) 
      <tr> 
       @foreach ($record as $col) 
        <td>{{ $col }}</td> 
       @endforeach 
      </tr> 
     @endforeach 
    </tbody> 
</table> 

控制器:(使用getColumns辅助功能)

$users = User::all(); 
// In the first array, just pass the fields 
// you want to get, second one is data array 
$data['records'] = getColumns(array('id', 'username', 'email'), $users->toArray()); 
return View::make('user.users', $data); 

或者,你可以使用它像这样

$users = User::all()->toArray(); 
// In this example, I want to show only 
// username, first_name and last_name 
$records = getColumns(array('username', 'first_name', 'last_name'), $users); 
return View::make('user.users')->with('records', $records); 

这是一个辅助功能,它可以使用这个从任何模型/控制器

function getColumns($keys, $result) 
{ 
    $records = array(); 
    foreach ($result as $row) { 
     $record = array(); 
     foreach ($row as $k => $v) { 
      if(in_array($k, $keys)) $record[$k] = $v; 
     } 
     $records[] = $record; 
    } 
    return $records; 
} 

我m使用帮助函数,因为我可以在任何模型中使用它,它在我的filters.php文件中,您可以将它放在任何地方。其实,我有一个文件custom_helpers.php文件app文件夹中filters.php和我filters.php文件的末尾,我有这个

include "custom_helpers.php"; 

在这种custom_helpers.php我和其他功能的辅助功能。下面是截图

$users = User::take(5)->get()->toArray(); 
$records = getColumns(array('username', 'first_name', 'last_name'), $users); 
return View::make('user.users')->with('records', $records); 

屏幕截图的例子: enter image description here

相关问题