在版本5.2中输入控制器闭包


Inputting to a Controllers closure in laravel 5.2

所以我有一个'TicketController',它包含了我在系统中操纵'票'的函数。我正在寻找最好的方法来发送我的新路线,它将采用{id}的路线参数到我的TicketController以查看票。

这是我的路线集

    Route::group(['middleware' => 'auth', 'prefix' => 'tickets'], function(){
    Route::get('/', 'TicketController@userGetTicketsIndex');
    Route::get('/new', function(){
       return view('tickets.new');
     });
    Route::post('/new/post', 'TicketController@addNewTicket');
    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
 {
}

这里不需要使用闭包。只需调用一个动作:

Route::get('/view/{id}', 'TicketController@showTicket');

TicketController中你会得到ID:

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

你应该在文章中使用type-hint。其可怕的
路由

Route::get('/view/{ticket}', 'TicketController@viewTicketDetails');
在控制器

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
 {