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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22182595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:43:15  来源:igfitidea点击:

how do i use include file with laravel?

phpincludelaravel-4

提问by Sunday

I am using Laravel and i found a chart lib for my project. This library(libchart) is php and it use this:

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

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

but if i put this code in my view, i get this error:

但是如果我把这段代码放在我的视图中,我会收到这个错误:

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

I dont know how i have to add code in the view or add the library in Laravel.

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

When i download the library, I have this:

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

libchart
->libchart
->demo

Then i paste in:

然后我粘贴:

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

And include "../libchart/classes/libchart.php";is:

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

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

EDIT:

编辑:

I tried this:

我试过这个:

  1. I create a directory called grafico in app.
  2. I did this:

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

  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); });

In return View::make('template', $data);which type of template i have to use? and i get this error now:

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

Class 'Libchart' not found

回答by hemant kumawat

Forget complex code of core PHP in larval because Laravel has provided a function that uses for add many files in your for blade template code,

忘记在 larval 中核心 PHP 的复杂代码,因为 Laravel 提供了一个函数,用于在您的刀片模板代码中添加许多文件,

@include('foldername.filename')

@include('filename')

They are pure Laravel functions.

它们是纯 Laravel 函数。

回答by Sunday

Finally i did it. Rahil Wazir helped me, and now my project is working.

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

My route:

我的路线:

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>

I added a folder named "librerias" in app and i pasted the library. Next, I went to:

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

myproject
->composer.json

I added this: "app/librerias" in

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

autoload
->classmap

Then i runned with cmd: composer dump-autoload and it worked.

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

回答by Rahil Wazir

Don't include your files in view folder.

不要将您的文件包含在视图文件夹中。

Create a custom folder in your appdirectory called anythingFolderand paste your library there.

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

Now include your file in your controller method from where you want to use it, like this:

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

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

Improve the above code as your need

根据需要改进上面的代码

Read this How to add a library folder to Laravel 4

阅读此如何将库文件夹添加到 Laravel 4