douji6461 2018-07-10 09:11
浏览 52

来自路由器的Laravel 5替代呼叫控制器

I need to show three pages, depending of request parameters. Now it is realised like in the docs :

This is idea, I omit some of code

Route:

Route::get('/home/{mode}', 'ModeController@show');

Controller :

switch (mode) {
  case 0 : // gather the data set 1
            return View::make("View one", compact('data set1'))
  case 1 : // gather the data set 2
            return View::make("View two", compact('data set2'))
  case 2 : // gather the data set 3
            return View::make("View three", compact('data set3))
} 

Sure, controller got thick and stupid. I'd like realize that like this

Route : 1. Analyze mode parameter 2. Call controller, depends of mode

Controller : Gather data and calling view

Is it possible ?

  • 写回答

1条回答 默认 最新

  • doujishan2247 2018-07-12 09:48
    关注

    Well, solution is as here : In web.php

    Route::get('data', 'data@show')->name('data.show');
    
    Route::get('/', function (request $request) {
    
    
    if (isset($request->mode)) {  
        $mode_prm = $request->mode;
    } else {
        $mode_prm = 0;
    }
    
    switch ($mode_prm) {        
        case 0 :
            return redirect()->route('data.show');
        case 1 :
        //and so on
    

    });

    Controller :

    class data extends Controller { //

     public function show(request $request){
        //gather and perform data into data set for the view 
        return view('data', compact(data_set));
    }
    

    }

    I don't know if that solution is beautiful, but it works. Hope it helps to someone.

    评论

报告相同问题?