Laravel controller Single Responsibility principle I have a method in Laravel controller. How can I improve this code? How can I implement single responsibilty here or maybe there are some other tips I can use in this code? I know that controller methods should be kept as small as possible,but I don't know if it's better idea to split this code in separate methods.

public function displayData(CityRequest $request)
    {
        if (isset($request-&gt;validator) &amp;&amp; $request-&gt;validator-&gt;fails()) {
            return redirect()-&gt;back()-&gt;withErrors($request-&gt;validator-&gt;messages());
        }
        else {
            $cityName = $request-&gt;city;
            $cityName = str_replace(' ','-',$cityName);
            $cityName = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $cityName);
            $response = $this-&gt;guzzle-&gt;request('GET', asset("/api/products/recommended/" . $cityName));
            $response_body = json_decode($response-&gt;getBody());
            if(isset($response_body-&gt;error))
            {
                return redirect()-&gt;back()-&gt;withErrors(['city'=&gt;$response_body-&gt;error]);
            }
            $city = $response_body-&gt;city;
            $condition = $response_body-&gt;current_weather;
            $products = $response_body-&gt;recommended_products;
            return back()-&gt;with(['city' =&gt; $city, 'condition' =&gt; $condition, 'products' =&gt; $products]);
        }
    }
Here are some simple tips on how you can improve your code. There are still more you can do to improve it, but is a good start and it helps you get closer to the single responsibility principle.


Move the API request to a service (App\Services\ProductsApi\Client).
Move the formatting of city to a helper (App\Helpers\Format).
Consider moving the formatting of city to CityRequest so the controller doesn't have to do it.
No need for new variables, use the properties returned by $response instead.
No need for else-statement, since the if-statement has return.


Your controller:

use App\Services\ProductsApi\Client;

public function __construct(Client $client)
{
    $this-&gt;client = $client;
}

public function displayData(CityRequest $request)
{
    if (isset($request-&gt;validator) &amp;&amp; $request-&gt;validator-&gt;fails()) {
        return redirect()-&gt;back()-&gt;withErrors($request-&gt;validator-&gt;messages());
    }

    $response = $this-&gt;client-&gt;getRecommended(
        App\Helpers\Format::slugify($request-&gt;city)
    );

    if ($response-&gt;error) {
        return redirect()-&gt;back()-&gt;withErrors([
            'city' =&gt; $response-&gt;error
        ]);
    }

    return back()-&gt;with([
        'city' =&gt; $response-&gt;city ?? null, 
        'condition' =&gt; $response-&gt;current_weather ?? null,  
        'products' =&gt; $response-&gt;recommended_products ?? null, 
    ]);
}


Helper:

public static function slugify($value)
{
    return Illuminate\Support\Str::slug(
        iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $value),
        '-'
    );
}


(None of the code above is tested)