2013-11-26 30 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

相關問題