Andrew
Andrew

Reputation: 846

Access middleware in laravel controller

i am create a currency converter middleware in laravel based on subdomain wise, app/Http/Middleware/Currency.php this middleware is used to convert currency

namespace App\Http\Middleware;

use Closure;

class Currency
{
public function convert($request, Closure $next)
{
$sub=array_shift((explode('.', $_SERVER['HTTP_HOST'])));
$fromCurrency =  "AED";
$toCurrency = "$sub";
$amount = "1";
$url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;
$get = file_get_contents($url);
$data = preg_split('/\D\s(.*?)\s=\s/',$get);
$exhangeRate = (float) substr($data[1],0,7);
$convertedAmount = $amount*$exhangeRate;
$data = array( 'exhangeRate' => $exhangeRate, 'convertedAmount' =>$convertedAmount,       'fromCurrency' => strtoupper($fromCurrency), 'toCurrency' => strtoupper($toCurrency));
return json_encode( $data );
    }
}

and write in Kernel.php like

protected $middleware = [
     \App\Http\Middleware\Currency::class,
];

and show Function name must be a string error also in page, how to access this return value in controller?

Upvotes: 1

Views: 240

Answers (2)

Sehdev
Sehdev

Reputation: 5662

To achieve your purpose you should use Helper instead of Middleware. As per Laravel Documenation

Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, Laravel includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

You can create a custom Helper as below and use it anywhere in your app

Step 1: Create your a Currency Helpers class file and give it a matching namespace. Write your class and method:

  <?php // Code within app\Helpers\Currency.php

    namespace App\Helpers;


class Currency
{
    public static function convert($request, Closure $next)
    {
        $sub=array_shift((explode('.', $_SERVER['HTTP_HOST'])));
        $fromCurrency =  "AED";
        $toCurrency = "$sub";
        $amount = "1";
        $url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;
        $get = file_get_contents($url);
        $data = preg_split('/\D\s(.*?)\s=\s/',$get);
        $exhangeRate = (float) substr($data[1],0,7);
        $convertedAmount = $amount*$exhangeRate;
        $data = array( 'exhangeRate' => $exhangeRate, 'convertedAmount' =>$convertedAmount,       'fromCurrency' => strtoupper($fromCurrency), 'toCurrency' => strtoupper($toCurrency));
        return json_encode( $data );
    }
}

Step 2: Create an alias:

<?php // Code within config/app.php

    'aliases' => [
     ...
        'Currency' => App\Helpers\Helper::class,
     ...

Step 3: Run composer dump-autoload in the project root

Step 4: Use it like this in your controller

<?php 

namespace App\Http\Controllers;

use Currency;



 class SomeController extends Controller
    {

        public function __construct()
        {
            Currency::convert($value);
        }

Upvotes: 1

Helioarch
Helioarch

Reputation: 1188

I do not think that middleware is the best way to implement the feature you want but to do what you asked using a middleware, the middleware itself must have a function called handle instead of convert. Also you could flash the result into your sessions to access it inside the controller.

Take note also the return of the handle function, as it is required for the process to continue

namespace App\Http\Middleware;

use Closure;

class Currency
{
    public function handle($request, Closure $next)
    {
        $explodedArr = explode('.', $_SERVER['HTTP_HOST']);
        $sub = array_shift($explodedArr);
        $fromCurrency =  "AED";
        $toCurrency = "$sub";
        $amount = "1";
        $url = "https://www.google.com/search?q=".$fromCurrency."+to+".$toCurrency;
        $get = file_get_contents($url);
        $data = preg_split('/\D\s(.*?)\s=\s/',$get);
        $exhangeRate = (float) substr($data[1],0,7);
        $convertedAmount = $amount*$exhangeRate;
        $data = array( 'exhangeRate' => $exhangeRate, 'convertedAmount' =>$convertedAmount,       'fromCurrency' => strtoupper($fromCurrency), 'toCurrency' => strtoupper($toCurrency));

        session()->flash('convert_result', json_encode($data) );
        return $next($request);
    }
}

// and you should be able to get the result in your controller like so
session('convert_result');

Upvotes: 1

Related Questions