Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Routing: How to route when using URL query string?

I have a route - let's call it stats. Here is what my routing currently looks like:

Route::get('stats', 'StatsController@index');
Route::get('stats/{query}', 'StatsController@store');

My goal is to show stat data if someone visits /stats and to store stat data if someone visits a URL similar to /stats?name=John&device=Android.

How would I then route if there is a query string attached to my namespace stats?

Something like this?

Route::get('stats/?name=*&device=*', 'StatsController@store');
like image 891
sparecycle Avatar asked Jul 02 '14 19:07

sparecycle


People also ask

How to generate route with query string parameter in Laravel 5?

How to generate route with query string parameter in Laravel 5? Whenever you require to add query string parameter in link url, i mean in route then you generate link with query string parameter using Laravel 5 URL facade. most of new developer doing this way: But you should do it that this way, bellow example you can see how it done.

How to redirect a route in Laravel?

Laravel default provides redirect () helper. redirect () helper provides method route for redirect named route. In Bellow’s example, the cart () function for the route handler. this function redirects home route with 2 parameters as query string like id and product_id, so URL will be like this way:

How to add query string in link URL in Laravel?

When we need to add any query string or parameter in our link url, here i mean that in the route then we generate a link also with the query string parameter by using Laravel URL facade. most of the new developer doing this way: <a href =" { { URL::route ('home') }}.'?id=1&test_id=2'" > Test </a> But we should do this it that way.

How does the route Helper Work in Laravel?

The route helper will automatically extract the model's route key: Laravel allows you to easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows Laravel to verify that the URL has not been modified since it was created.


1 Answers

routes.php

Route::get('stats', 'StatsController@index');

StatsController

public function index()
{
    if(Input::has('name') and Input::has('device')))
        return $this->store();

    // Show stat ...
}

public function store()
{
    $input = Input::only('name', 'device');

    // Store stat ...
}

although it seems a pefect scenario for a RESTFUL controller. Whoever is sending the input should do it with a POST request

like image 164
Javi Stolz Avatar answered Oct 04 '22 00:10

Javi Stolz