If url param then return a view else go to controller in Laravel route

If url param then return a view else go to the controller in Laravel route

我有一个设置会话变量并将用户发送到 staff.resultList 视图的 SearchController。有一个 link 可以将视图从列表视图切换到网格视图。我正在尝试添加一个条件语句来促进该切换。在我的路线文件中,我有:

Route::get('home/memberhome/search', function(){
    if(isset($_GET['resultView']) && Session::has('lastSearch')){
        if($_GET['resultView'] == 'grid'){
            return view('staff.resultsGrid');
        } else {
            return view('staff.resultList');
        }
    } else {
        'SearchController@getAvailablity';
    }
});

显然这是行不通的,但我不确定是否有正确的语法将其传递给 运行 控制器(如果条件不是本意,如果满足条件则仅显示视图。

我可以完成这项工作还是我应该使用其他方法?

路由文件

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
 */

use App\Http\Controllers;
// use Illuminate\Http\Request;
// use Mail;
Route::get('/', function () {
    return redirect('home/memberhome');
});


Route::get('/sso/{accessCode}', 'SsoController@ssoLogin');
Route::get('/ssofailure', 'SsoController@ssoFail');
// Route::get('/optimum/{searchString?}', 'OptimumController@getAutoComplete($searchString=""');

Route::get('home/memberhome/search', function(){
    if(isset($_GET['resultView']) && Session::has('lastSearch')){
        if($_GET['resultView'] == 'grid'){
            return view('staff.resultsGrid');
        } else {
            return view('staff.resultList');
        }
    } else {
        return redirect()->action('SearchController@getAvailablity');
    }
});
Route::group(['middleware' => ['auth', 'role:super_admin admin']], function () {
    // REPORTS ROUTES
    Route::get('dashboard/reports', 'ReportController@index');
    Route::get('dashboard/reports/filter', 'ReportController@filterReports');
    Route::get('dashboard/reports/export/{id}', 'ReportController@export');
    Route::get('dashboard/reports/{id}/bookings', 'ReportController@reports');
    Route::get('dashboard/reports/{id}/bookings/search', 'ReportController@searchBookings');
    Route::get('dashboard/client/search', 'ClientController@search');
    Route::get('dashboard/clients/create', 'ClientController@create');
    Route::post('dashboard/clients/store', 'ClientController@store');


    // INVOICE ROUTES
    Route::get('dashboard/invoices', 'InvoiceController@index');
    Route::post('dashboard/invoices', 'InvoiceController@store');
    Route::get('dashboard/invoices/edit/{id}', 'InvoiceController@edit');
    Route::post('dashboard/invoices/edit/{id}', 'InvoiceController@updateBooking');
    Route::get('dashboard/invoices/remove/{id}', 'InvoiceController@removeBooking');
    Route::get('dashboard/invoice/export/{id}', 'InvoiceController@export');
    Route::post('dashboard/invoices/{id}/paid', 'InvoiceController@markPaid');
    Route::get('dashboard/invoices/{id}', 'InvoiceController@show');

});

您可以像这样将用户重定向到另一个控制器的操作:

return redirect()->action('SearchController@getAvailablity');

所以它会是这样的:

Route::get('home/memberhome/search', function(){
    if(isset($_GET['resultView']) && Session::has('lastSearch')){
        if($_GET['resultView'] == 'grid'){
            return view('staff.resultsGrid');
        } else {
            return view('staff.resultList');
        }
    } else {
        return redirect()->action('SearchController@getAvailablity');
    }
});

更多信息:https://laravel.com/docs/5.8/redirects#redirecting-controller-actions

更新

如果你的控制器方法没有在任何路由中注册,你可以这样访问它:

return app(App\Http\Controllers\SearchController::class)->getAvailablity();

所以它会是这样的:

Route::get('home/memberhome/search', function(){
    if(isset($_GET['resultView']) && Session::has('lastSearch')){
        if($_GET['resultView'] == 'grid'){
            return view('staff.resultsGrid');
        } else {
            return view('staff.resultList');
        }
    } else {
        return app(App\Http\Controllers\SearchController::class)->getAvailablity();
    }
});