我如何在 Laravel 中使用包含文件?

问题描述 投票:0回答:4

我正在使用 Laravel,我为我的项目找到了一个图表库。这个库(libchart)是php,它使用这个:

include "../libchart/classes/libchart.php";

但是如果我将此代码放在我的视图中,我会收到此错误:

include(../libchart/classes/libchart.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

我不知道如何在视图中添加代码或在 Laravel 中添加库。

当我下载库时,我有这个:

libchart
->libchart
->demo

然后我粘贴:

myproject
->app
  ->views
    ->libchart
      ->libchart
      ->demo

并且

include "../libchart/classes/libchart.php";
是:

libchart
->libchart
->demo
  ->LineChartTest.php

编辑:

我试过这个:

  1. 我在应用程序中创建了一个名为 grafico 的目录。
  2. 我这样做了:

    Route::get('pruebaimagen', function() {
        include_once(app_path() . '/grafico/libchart/classes/libchart.php');
        $data['libchart'] = new Libchart();
        return View::make('template', $data);
    });

return View::make('template', $data);
中我必须使用哪种类型的模板?我现在收到此错误:

Class 'Libchart' not found
php include laravel-4
4个回答
8
投票

感谢 Laravel,您可以利用框架提供的内置功能简化向 Blade 模板添加多个文件的过程。

@include('foldername.filename')

@include('filename')

它们是纯 Laravel 函数。


6
投票

我终于做到了。 Rahil Wazir 帮助了我,现在我的项目正在运作。

我的路线:

Route::get('pruebaimagen', function() {
    include_once(app_path() . '/librerias/libchart/classes/libchart.php');
    return View::make('demo.LineChartTest');
});

我的看法:

<?php
/* Libchart - PHP chart library
 * Copyright (C) 2005-2011 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

/**
 * Line chart demonstration
 *
 */

$chart = new LineChart();

$dataSet = new XYDataSet();
$dataSet->addPoint(new Point("06-01", 273));
$dataSet->addPoint(new Point("06-02", 421));
$dataSet->addPoint(new Point("06-03", 642));
$dataSet->addPoint(new Point("06-04", 799));
$dataSet->addPoint(new Point("06-05", 1009));
$dataSet->addPoint(new Point("06-06", 1406));
$dataSet->addPoint(new Point("06-07", 1820));
$dataSet->addPoint(new Point("06-08", 2511));
$dataSet->addPoint(new Point("06-09", 2832));
$dataSet->addPoint(new Point("06-10", 3550));
$dataSet->addPoint(new Point("06-11", 4143));
$dataSet->addPoint(new Point("06-12", 4715));
$chart->setDataSet($dataSet);

$chart->setTitle("Sales for 2006");
$chart->render("recursos/demo5.png");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Libchart line demonstration</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" />
</head>
<body>
    <img alt="Line chart" src="generated/demo5.png" style="border: 1px solid gray;"/>
</body>
</html>

我在应用程序中添加了一个名为“librerias”的文件夹,并粘贴了该库。 接下来,我去了:

myproject
->composer.json

我添加了这个:“app/librerias”在

autoload
->classmap

然后我用 cmd 运行:composer dump-autoload 并且它起作用了。


4
投票

不要将您的文件包含在查看文件夹中。

在您的

app
目录中创建一个名为
anythingFolder
的自定义文件夹,并将您的库粘贴到其中。

现在将您的文件包含在您想要使用它的控制器方法中,如下所示:

public function index() {
    include_once(app_path() . '/anythingFolder/libchart/classes/libchart.php');
    $data['libchart'] = new Libchart()
    return View::make('template', $data);
}

根据需要改进上述代码

阅读本文 如何向 Laravel 4 添加库文件夹


-1
投票
@include('foldername.filename')

这是在 laravel 中查找文件和文件夹的最简单方法 使用这个include会找到该文件,无需提及大路径

© www.soinside.com 2019 - 2024. All rights reserved.