我想在我的导航栏中有一个下拉菜单,我可以在其中选择一种货币,我的应用程序中的所有价格都会转换为选定的货币,我知道我应该为此使用中间件,但我不知道如何开始。我正在使用 Fixer与 laravel-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: for my middleware and config file I've got the idea of language translation and I don't have an idea how to join it to
SWAPpackage in order to work! :\
谢谢。
最佳答案
App::setLocale() 用于语言目的。
不要使用中间件来设置 session 默认值。它会使你的路由文件膨胀。使用 View 编辑器进行输出。 https://laravel.com/docs/4.2/responses#view-composers
如果您没有 View 编写器提供程序,请运行此命令:
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 的自动加载功能。 在“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”方法,因为每个 session 只需设置一次语言环境。
/**
* @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');
}
}
关于php - 将货币选择器添加到 laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50422001/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
我正在开发一个创建网络博客的RubyonRails项目。我希望将一个名为featured的boolean数据库字段添加到Post模型中。该字段应该可以通过我添加的事件管理界面进行编辑。我使用了以下代码,但我什至没有在网站上显示另一列。$railsgeneratemigrationaddFeaturedfeatured:boolean$rakedb:migrate我是RubyonRails的新手,非常感谢任何帮助。我的index.html.erb文件中的相关代码(views):FeaturedPost架构.rb:ActiveRecord::Schema.define(:version=>
假设我有一个这样的单例类:classSettingsincludeSingletondeftimeout#lazy-loadtimeoutfromconfigfile,orwhateverendend现在,如果我想知道使用什么超时,我需要编写如下内容:Settings.instance.timeout但我宁愿将其缩短为Settings.timeout使这项工作有效的一个明显方法是将设置的实现修改为:classSettingsincludeSingletondefself.timeoutinstance.timeoutenddeftimeout#lazy-loadtimeoutfromc