在Laravel框架中包含PHP文件 [英] Including PHP files with Laravel framework

查看:251
本文介绍了在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的localhost上.我不确定是否需要与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天全站免登陆