【问题标题】:Adding currency picker to laravel将货币选择器添加到 laravel
【发布时间】:2018-10-29 12:36:53
【问题描述】:

我想在我的导航栏中有一个下拉菜单,我可以在其中选择一种货币,并且我的应用程序中的所有价格都转换为所选货币,我知道我应该为此使用中间件,但我不知道如何开始。我使用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);
    }
}

我还在配置文件夹中创建了一个currencies.php

<?php

return [
  'IDR' => [
      'name' => 'Indunesian Rupiah',
  ],
  'USD' => [
      'name' => 'U.S Dollar',
  ],
  'EUR' => [
      'name' => 'Euro',
  ],
];

我也将此添加到我的config\app.php

'currency' => 'IDR',

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

PS:对于我的中间件和配置文件,我有语言的概念 翻译,我不知道如何将它加入到SWAP 包中 为了工作! :\

问题

  1. 我尝试处理货币的方式是否正确?
  2. 下一步我还应该做什么?

谢谢。

【问题讨论】:

  • 为什么不使用Waavi/translation 进行翻译,使用Torann/laravel-currency 进行货币。它非常易于使用。您不必创建中间件或任何不必要的编码

标签: php laravel


【解决方案1】:

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

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

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

php artisan make:provider ComposerServiceProvider

在“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');
    });
}

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

    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/Support/helpers.php"
    ]

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

composer dump-autoload

在“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');
        }
    }
}

如果您需要将语言环境设置为一种货币,请更改“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');
    }
}

【讨论】:

  • 您好,感谢您的回复。你介意把你的代码分开,这样我就可以知道我应该放什么代码?那么路由呢?还有前端选择器(比如下拉菜单)?
  • @mafortis 为完整的逐步实施编辑了答案
  • 当我尝试转储自动加载时,我收到此错误:pasteboard.co/Hsqfkld.png
  • 嗨,您对这个问题有什么想法吗?
  • 您是否使用大写字母“S”创建了 Support 文件夹?如果是,那么可能是 Windows 上的“目录分隔符”的问题。尝试将“app\\Support\\helpers.php”放在composer.json中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多