我有一个SearchController,它设置一个会话变量并将用户发送到staff.resultList视图。有一个链接可将视图从列表切换到网格视图。我试图添加一个条件语句,以促进该切换。在我的路由文件中,我有:
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');
});
答案 0 :(得分:0)
您可以将用户重定向到另一个控制器的操作,如下所示:
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();
}
});