2016-08-16 26 views
1

所以我有一個'TicketController'它擁有我的功能來操縱系統中的'票'。 我正在尋找制定出發我的新路線的最佳方法,它將路線參數{id}傳遞給我的TicketController以查看票證。輸入到一個控制器閉包在laravel 5.2

這裏是我的路線設置

Route::group(['middleware' => 'auth', 'prefix' => 'tickets'], function(){ 

    Route::get('/', '[email protected]'); 
    Route::get('/new', function(){ 
     return view('tickets.new'); 
    }); 
    Route::post('/new/post', '[email protected]'); 
    Route::get('/new/post', function(){ 
     return view('tickets.new'); 
    }); 
    Route::get('/view/{id}', function($id){ 
     // I would like to ideally call my TicketController here 
    }); 
}); 

這裏是我的機票控制器

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

use App\Http\Requests; 
use App\Ticket; 
use App\User; 

class TicketController extends Controller 
{ 
    public function __construct() 
    { 
     $this->middleware('auth'); 
    } 

    /** 
    * Returns active tickets for the currently logged in user 
    * @return \Illuminate\Http\Response 
    */ 
    public function userGetTicketsIndex() 
    { 
     $currentuser = \Auth::id(); 
     $tickets = Ticket::where('user_id', $currentuser) 
      ->orderBy('updated_at', 'desc') 
      ->paginate(10); 
     return view('tickets.index')->with('tickets', $tickets); 
    } 

    public function userGetTicketActiveAmount() 
    { 
     $currentuser = \Auth::id(); 
    } 

    public function addNewTicket(Request $request) 
    { 
     $this->validate($request,[ 
      'Subject' => 'required|max:255', 
      'Message' => 'required|max:1000', 
     ]); 
     $currentuser = \Auth::id(); 
     $ticket = new Ticket; 
     $ticket->user_id = $currentuser; 
     $ticket->subject = $request->Subject; 
     $ticket->comment = $request->Message; 
     $ticket->status = '1'; 
     $ticket->save(); 
    } 

public function viewTicketDetails() 
{ 
    //retrieve ticket details here 
{ 

} 

回答

1

你不需要在這裏使用封閉。只需撥打一個動作:

Route::get('/view/{id}', '[email protected]'); 

而且在TicketController你會得到ID:

public function showTicket($id) 
{ 
    dd($id); 
} 

更多關於此here

+0

Alrite布里爾。說實話,除非我錯過了一些文件只是關閉使用封閉。感謝分配給我清理這個。標記爲正確(顯然) –

1

你應該在laravel中使用type-hint。它真棒
在路線

Route::get('/view/{ticket}', '[email protected]'); 

在控制器

public function viewTicketDetails(Ticket $ticket) 
{ 
    //$ticket is instance of Ticket Model with given ID 
    //And you don't need to $ticket = Ticket::find($id) anymore 
{ 
+0

如果您正在使用模型,這是非常好的選擇! :) –

相關問題