我如何使用laravel包含文件?

时间:2014-03-04 20:42:41

标签: php include laravel-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. 我在app中创建了一个名为grafico的目录。
  2. 我这样做了:

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

  3. return View::make('template', $data);我必须使用哪种模板?我现在得到这个错误:

    Class 'Libchart' not found
    

3 个答案:

答案 0 :(得分:3)

忘记了幼虫中核心PHP的复杂代码,因为Laravel提供了一个函数,用于在你的for blade模板代码中添加许多文件,

@include('foldername.filename')

@include('filename')

它们是纯粹的Laravel功能。

答案 1 :(得分:2)

最后我做到了。 Rahil Wazir帮助了我,现在我的项目正在进行中。

我的路线:

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

Mi view:

<?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>

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

myproject
->composer.json

我在

中添加了这个:“app / librerias”
autoload
->classmap

然后我使用cmd:composer dump-autoload运行它并且它有效。

答案 2 :(得分:0)

请勿将您的文件包含在视图文件夹中。

在名为app的{​​{1}}目录中创建一个自定义文件夹,然后将您的图书馆粘贴到那里。

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

anythingFolder

根据需要改进上述代码

阅读此How to add a library folder to Laravel 4