首页 > 解决方案 > Laravel 将 php 类导入刀片视图

问题描述

我想在我的刀片视图中包含一个自己编写的 ‍‍<code>.‍php 文件,这样我就可以在类中运行该方法。

它应该可以用作 var。以某种方式对我来说或印在我看来。

稍后我需要它以便在我的 web 应用程序中实现 php 函数和方法(编写一个从存储路径加载 .csv 文件的工具,该文件包含我想要提取并保存在我的数据库中的信息)。

App\CustomClass\downloadLog.php

namespace App\CustomClass;

use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Hash;

use App\User;

class downloadLog
{
    public static function  save_log()
    {
        $string = "this is just a string";
        print($string);
    }
}

资源/视图/log.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head></head>
<body>
    <div>I wanna see for instance my printed string here</div>
</body>
</html>

路线/web.php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/log', function() {
    return view ('log');
});

Route::get('/dashboard', function() {
    return view ('menu');
});

Route::get('/search', function() {
    return view ('searchbar');
});
Route::get('/games', function() {
    return view ('searchbar');
});

Route::get('/data', function() {
    return view ('searchbar');
});

标签: phpdatabaselaravelclass

解决方案


请尝试@php downloadLog::save_log('string') @endphp您的刀片文件

有关更多信息,请参阅https://laravel.com/docs/7.x/blade#php

这个答案如何使用刀片模板中的类?


推荐阅读