2016-10-08 46 views
1

我用laravel自定義表單要求與命令php artisan make:request AddressBookRequest
和使用,要求在我的控制器,如:
路線在自定義表單請求沒有找到

public function add_address_book($lang,$user_id,AddressBookRequest $request){ 
dd($request); 
} 

當我運行API路線laravel顯示:

NotFoundHttpException in RouteCollection.php line 161: 

但是,當我改變AddressBookRequestRequest喜歡:

public function add_address_book($lang,$user_id,Request $request){ 
dd($request); 
} 

阿比工作正常

AddressBookRequest:

<?php namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class AddressBookRequest extends Request 
{ 
/** 
* Determine if the user is authorized to make this request. 
* 
* @return bool 
*/ 
public function authorize() 
{ 
    return true; 
} 

/** 
* Get the validation rules that apply to the request. 
* 
* @return array 
*/ 
public function rules() 
{ 
      return [ 
       'title' => 'required', 
       'address' => 'required', 
       'latitude' => 'required', 
       'longitude' => 'required' 
      ]; 
} 

public function messages() 
{ 
    return [ 
       'title.required' => trans('address_book.title_required'), 
       'address.required' => trans('address_book.address_required'), 
       'latitude.required' => trans('address_book.latitude_required'), 
       'longitude.required' => trans('address_book.longitude_required'), 
    ]; 
} 


} 

AddressBookController usecases:

<?php namespace App\Http\Aggregate\Address_book\Controller\v1_0; 

use App\Http\Requests\AddressBookRequest; 
use Illuminate\Routing\Controller as BaseController; 
use EventHomes\Api\ApiController; 
use JWTAuth; 

class AddressBookController extends BaseController 
{ 

和路由:

Route::group(['namespace' => 'Aggregate\Address_book\Controller\v1_0', 'middleware' => 'jwt.auth', 'prefix' => 'api/v1.0/{lang}'], function() { 
    Route::post('customer/{id}/address_book', '[email protected]_address_book'); 
    }); 

我如何解決它以使用自定義請求?

任何幫助將不勝感激

+0

一下添加到控制文件的頂部**使用App \ HTTP \請求\ AddressBookRequest; ** – Anish

+0

我補充,但它仍然無法正常工作 – amirali

+0

你現在做了什麼錯誤? – Anish

回答

1

你應該加入這一行控制器的頂部:

use App\Http\Requests\AddressBookRequest; 

此外,還要確保自定義的請求類中authorize()方法返回true

public function authorize() 
{ 
    return true; 
} 
+0

我已添加但仍不起作用 – amirali

+0

您現在得到什麼錯誤?請顯示您使用的路線。 –

+0

同樣的錯誤,route:'Route :: group(['namespace'=>'Aggregate \ Address_book \ Controller \ v1_0','middleware'=>'jwt.auth','prefix'=>'api/v1.0/{lang}'],function(){ Route :: post('customer/{id}/address_book','AddressBookController @ add_address_book'); }); – amirali

0

我修復它通過添加:

use Illuminate\Foundation\Http\FormRequest; 
use EventHomes\Api\ApiController; 

abstract class Request extends FormRequest 
{ 
    use ApiController; 

    public function response(array $errors) 
    { 
     foreach($errors as $key=>$error) 
     { 
     return $this->respondUnprocessable(1004,'validation',$errors[$key][0]); 
     } 
    } 

} 

requst.php