如何在.blade.PHP文件Laravel中使用PHP回显字符串

如何在.blade.PHP文件Laravel中使用PHP回显字符串,php,laravel-5.7,Php,Laravel 5.7,我正在尝试使用 LTC$。 XMR$。 @php和@endphp之间的代码运行良好,但是当我添加 长期护理$ XMR$ 页面加载,但不加载值。我做错了什么?在视图中做任何事情都是在与框架作斗争。如果您打算这样做,不妨使用普通的旧php 尽管如此,如果您确实不想使用控制器,您可以在路由文件web.php中执行API查找,然后将数据传递给视图,从而使逻辑保持一定的分离 Route::get('/', function () { $response = json_decode(fil

我正在尝试使用


LTC$。
XMR$。
@php和@endphp之间的代码运行良好,但是当我添加

长期护理$
XMR$

页面加载,但不加载值。我做错了什么?

在视图中做任何事情都是在与框架作斗争。如果您打算这样做,不妨使用普通的旧php

尽管如此,如果您确实不想使用控制器,您可以在路由文件
web.php
中执行API查找,然后将数据传递给视图,从而使逻辑保持一定的分离

Route::get('/', function () {

    $response = json_decode(file_get_contents("https://api.coinmarketcap.com/v1/ticker/"),true);
    $currencyValues = array_column($response, 'price_usd', 'id');

    // Now call your view and pass in the data so the view has access to it. 
    // Notice that the view will have access to a variable named `$currencies` and not `$currencyValues` 
    return view('welcome', [
        'currencies' => $currencyValues
    ]);
});
然后,在您的视图中,
welcome.blade.php
,您可以输出检索到的货币

编辑-忘了我们在Blade工作。添加了
{…}

{{$currences['bitcoin']}

{{$货币['litecoin']}
希望您知道,这其中的一半一开始就不属于模板。我花了3个小时试图找出如何使用路由/控制器,但运气不佳,试图在github上遵循这一点和其他一些东西,但却迷路了。这是我的目标,至少可以做到这一点,只需要让echo命令正常运行即可工作
只含糊地提到你“得到了错误”,而不告诉我们这些错误实际上是什么,这是毫无意义的。这是一个来自你的
price
函数的错误-显然
$decoded_json
不是你所期望的,而是空的。您可以检查
json\u last\u error
/
json\u last\u error\u msg
以了解尝试解码json时出现的错误。(最有可能的情况是,尝试使用file_get_contents获取数据时返回的不是预期的JSON而是错误消息,或者只是一个空字符串,因为在网络级别出现了故障。)所以,我只想确保我做得对,第一部分我应该放在routes文件夹中web.php的末尾,第二部分我应该放在welcome.blade.php中我想显示的地方?未定义变量:货币(View:/var/www/website/resources/views/welcome.blade.php)ErrorException(E_ERROR)对。但是您需要将
Route::get('/',function(){…}
替换为我编写的。不要只将它粘贴到文件的末尾。您应该只有一个
Route::get('/',…)
的定义。这些路由对于Laravel(以及大多数web框架)来说是基本的,所以我建议您阅读它们。Route::name('auth')->group(function(){include'auth.php';});Route::prefix('admin')->group(function(){Route::middleware(['admin_panel_access'])->group(function(){include'admin.php';});//Profile routes Route::middleware(['auth'])->group(function(){Route::get('banked','ProfileController@banned')->name('profile.banked');Route::middleware(['is_banked'])->group(function(){include'profile.php';});})Route::get('/','IndexController@home')->名称('home');
<center><h2>BTC $<?php= price("bitcoin");?></h2></center>
<center><h2>LTC $<?php= price("litecoin");?></h2></center>
<center><h2>XMR $<?php= price("monero");?></h2></center>
Route::get('/', function () {

    $response = json_decode(file_get_contents("https://api.coinmarketcap.com/v1/ticker/"),true);
    $currencyValues = array_column($response, 'price_usd', 'id');

    // Now call your view and pass in the data so the view has access to it. 
    // Notice that the view will have access to a variable named `$currencies` and not `$currencyValues` 
    return view('welcome', [
        'currencies' => $currencyValues
    ]);
});
{{ $currencies['bitcoin'] }}

<br />

{{ $currencies['litecoin'] }}