2015-04-22 33 views
6

Huzurlu bir API oluşturmaya çalışıyorum ve düşündüğüm bir noktaya geldim ve REST bunun hakkında çok açık değil, bir kaynağın birden çok URL tarafından erişilebilir olması gerektiğine inanıyorum. Bir örnek vereyim./Aynı denetleyiciye birden çok yol

localhost/forum/kategoriler/{category_id}

veya bu url ile tartışmalar

:

Route::group(['prefix' => 'forum'], function(){ 

    Route::resource('categories', 'CategoriesController'); 

    Route::resource('categories.discussions', 'DiscussionsController'); 

    Route::resource('discussions', 'DiscussionsController'); 

}); 

Niyetim ait olduğu kategoriye ekleyerek ya her tartışmaya erişmek mümkün olduğunu

localhost/forum/tartışma

tam kontrolör yeniden yazmadan çalışmak için böyle bir şey var herkes Has.

class DiscussionsController extends ApiController { 


public function __construct(DiscussionRepositoryInterface $discussion) 
{ 
    $this->discussions = $discussion; 
} 

/** 
* Display a listing of the resource. 
* 
* @return Response 
*/ 
public function index($category = null) 
{ 
    $response = $this->discussions->all($category); 

    return $this->respond($response->getData()); 
} 

/** 
* Show the form for creating a new resource. 
* 
* @return Response 
*/ 
public function create() 
{ 
    if (!$this->hasPermission('forum.create.discussion')) 
    { 
     return $this->respondNotAllowed(); 
    } 

    return $this->respondSuccess('Granted'); 
} 

/** 
* Store a newly created resource in storage. 
* 
* @return Response 
*/ 
public function store($category) 
{ 
    if (!$this->hasPermission('forum.create.discussion')) 
    { 
     return $this->respondNotAllowed(); 
    } 

    return $this->respondSuccess('Granted'); 

} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function show($categories = null, $discussions) 
{ 
    $discussion = $this->discussions->find($categories, $discussions); 

    if ($discussion == null) 
     return $this->respondNotFound('Discussion not found'); 

    $data = [ 
     'discussions' => $discussion 
    ]; 

    return $this->respond($data); 
} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return Response 
*/ 
public function edit($id) 
{ 
    if (!$this->hasPermission('forum.edit.discussion')) 
    { 
     return $this->respondNotAllowed(); 
    } 

    return $this->respondSuccess('Granted'); 
} 

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

    if (!$this->hasPermission('forum.edit.discussion')) 
    { 
     return $this->respondNotAllowed(); 
    } 

    $data = Input::only('category_id', 'user_id', 'desc', 'title'); 

    $data = $this->clearNullInput($data); 

    if ($this->discussions->update($id, $data)) 
     return $this->respondError('Couldnt update discussion'); 

    return $this->respondSuccess('Discussion updated successfully'); 
} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return Response 
*/ 
public function destroy($id) 
{ 
    if (!$this->hasPermission('forum.delete.discussion')) 
    { 
     return $this->respondNotAllowed(); 
    } 

    if (!$this->discussions->delete($id)) 
     return $this->respondError('Couldnt destroy user'); 

    return $this->respondSuccess('Granted'); 
} 

} 

indeks yöntemi çalışır her iki çağrıda (localhost/forum/kategoriler/id/tartışmalar ve localhost/forum/tartışma)

Ama ince:

Bu benim tartışmalar kontrolcüsüdür kaynak göstermeye çalışır, ancak uzun versiyon kısa bir aşağıdaki execption atar çalışır:

ErrorException in DiscussionsController.php line 68: 
Missing argument 2 for App\Http\Controllers\DiscussionsController::show() 
onun tanıyamıyor nedeniyle laravel deli gider söyleyebilirim

id, zeki bir geçici çözüm var mı yoksa denetleyiciyi yeniden yazmam gerekecek mi?

Herhangi görüş

derece sonra her iki değişkenleri kendi kesin değerlere sahip sen localhost/forum/categories/{categories}/discussions/{discussions} aradığınız takdirde bu

public function show(Category $category, Discussion $discussion) 

gibi bir şey deneyebilirsiniz sayesinde

cevap

3

apreciated olduğunu. Eğer localhost/forum/discussions/{discussions} ararsanız o zaman Kategori

Eğer

$router->model('discussions', 'App\Discussion'); 
$router->model('categories', 'App\Category'); 
+0

bir cazibe gibi çalıştı gibi RouteServiceProvider değerleri bağlayacaktır emin olun sadece yeni Kategori olacak! İlk başta model bağlama ile aynı problemi yaşayacağımı düşündüm, sanırım Laravel büyüsü – Borjante

İlgili konular