2013-08-05 82 views
2

在我route.php我有:Laravel 4:資源控制與前綴

Route::group(array('prefix'=>'admin'),function(){ 
    Route::resource('products','AdminProductsController'); 
}); 

但是,當我做STORE函數,它是一個POST,它拋出一個MethodNotAllowedHttpException,但它運作良好,對所有GET功能。我的表格action的價值是{{ URL::to('admin/products/store') }}

AdminProductsController.php位於controller/admin目錄中。

請幫忙。

控制器:

<?php 

class AdminProductsController extends BaseController { 

/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index() 
{ 
    return 'Yow'; 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return Response 
*/ 
public function create() 
{ 
    $url = URL::to('admin/products/store'); 

    return<<<qaz 
    <form method="post" action="{$url}"> 
     <input type="hidden" name="hehehe" value="dfsgg" /> 
     <input type="submit" /> 
    </form> 
qaz; 
} 

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

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function show($id) 
{ 
    return $id; 
} 

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

/** 
* Update the specified resource in storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function update($id) 
{ 
    // 
} 

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

} 
+1

我想你有一個錯字,不應該是:Route :: resource('products','AdminProductsController');'? – msturdy

+0

你能分享控制器中的代碼嗎?專門針對你提到的'store'行爲? – msturdy

+0

請再次檢查 – Orvyl

回答

1

訣竅是在你的例子中使用的路線。

試試這個

<form method="post" action="{{ URL::route('admin.products.store') }}"> 

現在你應該是好去!

建議:看看網址/動作/命名路線在Laravel - 可能是有用的。

0

Laravel docs,在store動作通過控制器的根路徑訪問(在你的情況/admin/products),而是由POST動詞觸發的,而不是GET,這將觸發index操作。

換句話說,如果你更新$url你在你的create()動作設置爲形式的action

public function create() 
{ 
    $url = URL::to('admin/products'); 

    return<<<qaz 
    <form method="post" action="{$url}"> 
     <input type="hidden" name="hehehe" value="dfsgg" /> 
     <input type="submit" /> 
    </form> 
qaz; 
} 

這應該只是工作。 :)

查看上述鏈接中的Verb | Path | Action | Route Name表以瞭解更多詳情。 path列顯示您可以訪問操作的位置。