使用 Laravel 框架包含 PHP 文件 [英] Including PHP files with Laravel framework

查看:20
本文介绍了使用 Laravel 框架包含 PHP 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 php 脚本包含"到我的一个视图 (landing.blade.php) 中.

I am trying to "include" a php script into one of my views (landing.blade.php).

脚本在:

/laravel-master/public/assets/scripts/config.php

当我尝试在视图中包含此代码时:

When I try to include this code in the view:

<?php include_once('/assets/scripts/config.php'); ?>

我得到错误:include_once(/assets/scripts/config.php): failed to open stream: No such file or directory

这是在本地主机上使用 MAMP.我不确定是否需要在 Laravel 4 中使用一组不同的规则来包含一个 php 文件.感谢您的帮助!

This is on localhost using MAMP. I'm not sure if there is a different set of rules I need to use with Laravel 4 to include a php file. Thank you for your help!

推荐答案

首先,不建议您将 PHP 文件保存在 public 目录中,它们应该保存在 中app 文件夹.我建议您在 app 中创建一个文件夹,例如 includes 并将您的文件放在那里.然后,包含它,执行以下操作:

First, it's not really recommended that you keep your PHP files in the public directory, they should be kept inside the app folder. I'd suggest you create a folder inside app, something like includes and put your files there. Then, you include it, do:

include(app_path().'/includes/config.php');

虽然,由于您似乎正在尝试加载一些配置文件,我还是建议您查看 Laravel 自己处理配置的方式.例如,如果你在 app/config 文件夹中创建了一个 myapp.php 文件,Laravel 会自动为你处理它,只要你有一些关键 -值对,像这样:

Although, since it looks like you're trying to load some configuration files, I'd recommend you also check out Laravel's own way of handling configurations. For instance, if you created a myapp.php file inside the app/config folder, Laravel would handle it automatically for you, as long as you'd have some key-value pairs, like this:

<?php

return [
    'name'     => 'Raphael',
    'gorgeous' => true
];

然后您可以使用 Config 类检索这些值:

You could then retrieve these values using the Config class:

Config::get('myapp.name'); // Raphael

这是一个更好的解决方案,因为您还可以利用 Laravel 的 环境配置.

This is a better solution because you can also take advantage of Laravel's environment configuration.

这篇关于使用 Laravel 框架包含 PHP 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆