Consolidate search logic into one method/route

Put both show search and run search into the same route. This prevents
Lynx from thinking the search results were from a GET request and
caching the contents of the search page.
This commit is contained in:
St John Karp 2019-02-19 19:57:20 +00:00
parent 912929cf02
commit d3927063c5
3 changed files with 16 additions and 40 deletions

View File

@ -12,17 +12,23 @@ use Illuminate\Http\Request;
class SearchController extends Controller class SearchController extends Controller
{ {
/** /**
* Show the page that lets users search across Accounts and Statuses. * Process a search request.
* *
* @return Illuminate\View\View The search page. * @param Request $request The POST request with search parameters.
*
* @return Illuminate\Routing\Redirector Redirect to the search page.
*/ */
public function show_search() public function search(Request $request)
{ {
if (session()->has('results')) $user = session('user');
{
# The user is coming here after peforming a search.
$results = session('results'); # Verify we have an actual search term.
if ($request->has('search_term'))
{
# Query the search end-point.
$results = Mastodon::domain(env('MASTODON_DOMAIN'))
->token($user->token)
->get('/search', ['q' => $request->search_term]);
} }
else else
{ {
@ -36,30 +42,4 @@ class SearchController extends Controller
return view('search', $vars); return view('search', $vars);
} }
/**
* Process a search request.
*
* @param Request $request The POST request with search parameters.
*
* @return Illuminate\Routing\Redirector Redirect to the search page.
*/
public function search(Request $request)
{
$user = session('user');
# Verify we have an actual search term.
if (!$request->has('search_term'))
{
abort(400);
}
# Query the search end-point.
$results = Mastodon::domain(env('MASTODON_DOMAIN'))
->token($user->token)
->get('/search', ['q' => $request->search_term]);
return redirect()->route('show_search')
->with('results', $results);
}
} }

View File

@ -3,6 +3,6 @@
<li><a href="{{ route('home') }}">Timeline</a></li> <li><a href="{{ route('home') }}">Timeline</a></li>
<li><a href="{{ route('public') }}">Public Timeline</a></li> <li><a href="{{ route('public') }}">Public Timeline</a></li>
<li><a href="{{ route('notifications') }}">Notifications</a></li> <li><a href="{{ route('notifications') }}">Notifications</a></li>
<li><a href="{{ route('show_search') }}">Search</a></li> <li><a href="{{ route('search') }}">Search</a></li>
</ul> </ul>
</nav> </nav>

View File

@ -71,11 +71,7 @@ Route::get('/account/{account_id}/unfollow', 'AccountController@unfollow_account
->name('unfollow') ->name('unfollow')
->middleware('authorize'); ->middleware('authorize');
Route::get('/search', 'SearchController@show_search') Route::match(['get', 'post'], '/search', 'SearchController@search')
->name('show_search')
->middleware('authorize');
Route::post('/search', 'SearchController@search')
->name('search') ->name('search')
->middleware('authorize'); ->middleware('authorize');