使用 Laravel 框架包含 PHP 文件

2023-10-31php开发问题
1

本文介绍了使用 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 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17