2015-03-25 52 views
0

我創建了一個ProductController的資源在route.php文件是這樣的: -Laravel 5.0 |更改默認行爲的名字是不工作的資源控制器

Route::resource('products','ProductController',['names' => ['create' => 'products.add']]); 

這裏是我的productController.php文件看起來像: -

<?php namespace lvtest\Http\Controllers; 

use lvtest\Http\Requests; 
use lvtest\Product; 
use lvtest\Http\Controllers\Controller; 

use Illuminate\Http\Request; 

class ProductController extends Controller { 


/** 
* Class constructor .. requires authentication 
*/ 

public function __construct() 
{ 
$this->middleware('auth'); 
} 
/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
$products = Product::all(); 
return view('products.productsList', ['products' => $products]); 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return Response 
*/ 
public function add() 
{ 
return 'Add a product'; 
} 

/** 
* Store a newly created resource in storage. 
* 
* @return Response 
*/ 
public function store() 
{ 
return 'store new product?'; 
} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function show($id = null) 
{ 
// $products = Product::all(); 
// print_r($products); 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function edit($id) 
{ 
// 
} 

/** 
* Update the specified resource in storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function update($id) 
{ 
return 'store new product?'; 
} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function destroy($id) 
{ 
// 
} 

} 

,並通過了「名」陣列,以改變的默認名稱創建方法添加。 當我到localhost:8000/products/add時,我得到一個空白頁。 如何解決這個

回答

2

添加['names' => ['create' => 'products.add']你的資源路由只改變路由名稱,而不是路徑方法的。這意味着您可以將您的路線稱爲route('products.add'),它將指向您的控制器上的create()方法。

當您使用Route::resource Laravel會預期您的控制器上有一個create()方法。爲了能夠做到你的建議,你可能需要添加該方法到你的控制器,然後添加一個單獨的路線:

Route::resource('products','ProductController'); 
Route::get('products/add', ['as' => 'products.add', 'uses' => '[email protected]']); 
+0

感謝您的回覆。 我知道這種方法,但我想知道是否有一種方法可以使用靜態方法**資源**。 +1 – 2015-03-25 12:13:43

+0

它也適用於我使用Route ::獲取路線::資源的頂部? – 2015-11-23 08:46:15

+0

但在PHP工匠路線中也提到了創建路由:列表 – 2015-11-23 08:47:05