将货币选择器添加到laravel

maf*_*tis 8 php laravel

我想在我的导航栏中有一个下拉列表,我可以选择一种货币,我的应用程序中的所有价格都转换为选定的货币,我知道我应该使用中间件来解决这个问题,但我不知道如何开始.我使用Fixerlaravel-swap作为汇率的一揽子计划.

我做了什么

我已经制作了一个名为它的中间件Currancy,它的内容是:

<?php

namespace App\Http\Middleware;

use Closure;
use Swap\Swap;
use Swap\Builder;

class Currancy
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Session::has('appcurrency') AND array_key_exists(Session::get('appcurrency'), Config::get('currencies'))) {
            $currency = Session::get('appcurrency'); 
            App::setLocale($currency);
        }
        else {
          App::setLocale(Config::get('app.currency'));
        }
        return $next($request);
    }
}
Run Code Online (Sandbox Code Playgroud)

我还在currencies.phpconfig文件夹中创建了一个:

<?php

return [
  'IDR' => [
      'name' => 'Indunesian Rupiah',
  ],
  'USD' => [
      'name' => 'U.S Dollar',
  ],
  'EUR' => [
      'name' => 'Euro',
  ],
];
Run Code Online (Sandbox Code Playgroud)

我也把这个添加到我的 config\app.php

'currency' => 'IDR',
Run Code Online (Sandbox Code Playgroud)

在这种情况下我的默认货币是IDR除非用户选择其他货币.

PS:对于我的中间件和配置文件,我已经有了语言翻译的想法,我不知道如何加入它来SWAP打包以便工作!:\

问题

  1. 我试图以正确的方式处理货币的方式是什么?
  2. 我还应该为下一步做些什么呢?

谢谢.

N69*_*69S 3

App::setLocale() 用于语言目的。

不要使用中间件来设置会话默认值。它会使你的路线文件膨胀。使用视图编辑器进行输出。https://laravel.com/docs/4.2/responses#view-composers

如果您没有视图编辑器提供程序,请运行此命令:

php artisan make:provider ComposerServiceProvider
Run Code Online (Sandbox Code Playgroud)

在“app/Providers/ComposerServiceProvider.php”中的“boot()”方法中

public function boot()
{
    View::composer(array('header','footer'), function($view)
    {
        if (!currentCurrency()) {
            setCurrency(config('app.currency'));
        }
        $view->with('currencies', config('currencies');
    });
}
Run Code Online (Sandbox Code Playgroud)

定义一些辅助函数。要加载助手,请使用作曲家的自动加载功能。在“composer.json”中的“autoload”属性紧,在“psr-4”之后:它将加载“app/Support/helpers.php”作为示例。

    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Support/helpers.php"
    ]
Run Code Online (Sandbox Code Playgroud)

更改“composer.json”后,使用以下命令重新生成自动加载文件:

composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

在“app/Support/helpers.php”(创建它)中添加以下函数:

<?php

if (!function_exists('currentCurrency')) {
    /**
     * @return string
     */
    function currentCurrency(){
        if (Session::has('appcurrency') AND array_key_exists(Session::get('appcurrency'), config('currencies'))) {
            return Session::get('appcurrency');
        }
        return '';
    }
}

if (!function_exists('setCurrency')) {
    /**
     * @param string $currency
     * @return void
     * @throws \Exception
     */
    function setCurrency($currency){
        if (array_key_exists($currency, config('currencies'))) {
            Session::set('appcurrency', $currency);
        } else {
            throw new \Exception('not a valid currency');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要将区域设置设置为其中一种货币,请更改“setCurrency”方法,因为每个会话只需设置一次区域设置。

/**
* @param string $currency
* @return void
* @throws \Exception
*/
function setCurrency($currency){
    if (array_key_exists($currency, config('currencies'))) {
        Session::set('appcurrency', $currency);
        App::setLocale($currency);
    } else {
        throw new \Exception('not a valid currency');
    }
}
Run Code Online (Sandbox Code Playgroud)