How to use external classes and PHP files in Laravel Controller?

 

By: Povilas Korop

Laravel is an MVC framework with its own folder structure, but sometimes we want to use something external which doesn’t follow the same structure. Let’s review two different scenarios – when we have external class and when it’s just a .php file.

Let’s say we have a simple example, a PagesController.php file like this:

 

 

Pretty simple, right? Now, let’s say we want to have our product prices on the homepage, but they come from some kind of external class or PHP file.

Use an external class in Controller

Let’s say we have a simple class to define the prices:

 

 

Now, where to put this class and how to use it? A couple of steps here:

1. You can put a class itself anywhere you want within \App folder

By default, Laravel offers you some folders there like Providers, but I personally prefer to create a separate one – like App\Libraries, App\Classes or App\Services. Or you can call it your own application – App\MyApp. This is totally your choice.

So, in this example, let’s save the class as App\Classes\PricesClass.php.

2. Namespace within the file

Now we have to tell Laravel what is the namespace of this new file – it’s the same as the folder:

 

 

3. Use the class in your Controller

Let’s get back to our PagesController.php – here we have to add use statement for that external class, and then we’re free to use it! Like this:

 

 

That’s it, nothing more complicated than that.

Have you tried our tool to generate Laravel adminpanel without a line of code?
Go to QuickAdminPanel.com

Use an external PHP file in Controller

Another case – it’s not always an OOP file that we want to use. For some cases it’s just a list of functions. Of course, we can wrap them in a class as well, but not always. So, how to use the same function, if we just have a prices.php file like this:

 

 

And that’s it – no class, no namespace, nothing.
Let’s place our function as app/functions/prices.php file. Then – we have three differentways of include it:

1. Just include the class with PHP functions like include() or require() – and don’t forget app_path() function:

 

 

Note that you still need a slash symbol before the folder functions.
You can read more about app_path() and other Helper functions in the official documentation.

2. In composer.json file you just add needed files in “autoload” section – in a new entry called “files”:
(thanks for this suggestion to the commenters Joseph and Hisham)

 

 

This way you don’t need to use any include() functions anywhere within your controllers – just use the functions straight away.

3. Autoload the whole folder in composer.json
(thanks to YOzaz for pointing this out in comments)

Another way is just autoload the folder with that file – so you would place any similar external “helpers” in that folder, and that would be included in the future. In this case – add this folder in array or “classmap”:

 

 

Choose this option if you want those files to be included in a manner of “set it and forget it”.

Notice: if you make these changes to composer.json file, don’t forget to run composer dump-autoload for changes to take effect.

Check out my new online course: Laravel Eloquent: Expert Level 

 

转载于:https://www.cnblogs.com/mouseleo/p/9979418.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/248997.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

在多种浏览器中嵌入Applet

1 基本信息 摘要:一个开发好的Java Applet,是通过标准的标签(Tag)嵌入到HTML页面中的。浏览器在解析到支持的嵌入Applet的标签时,会启动Java Plug-in来渲染标签中的Java Applet。 由于历史原因,在页面中嵌…

share团队冲刺3

团队冲刺第三天 昨天:完成了对输出文字,按钮控件的添加,能够将其在模拟器上运行 今天:学习输入的添加方式 问题:Android resource linking failed 在改变按钮样式的时候,出现了这个问题,正在百度…

Django学习手册 - ORM 数据创建/表操作 汇总

ORM 查询的数据类型: QuerySet与惰性机制(可以看作是一个列表) 所谓惰性机制:表名.objects.all()或者.filter()等都只是返回了一个QuerySet(查询结果集对象),它并不会马上执行sql,而是当调用QuerySet的时候才执行。 QuerySet特点&…

applet实现大文件ftp上传(三)

下一步呢&#xff0c;当然就是对这个jar文件进行数字证书包装。流程如下&#xff1a; <1>、生成密匙证书(key certificate)&#xff0c;该证书将存储在你的.keystore文件中。Validity指的是密匙的有效期&#xff0c;默认是180&#xff0c;但是这里我们需要一年的时间&am…

网站301重定向 解决方法

网站301重定向&#xff0c;一般都是将不带www的域名转向带有www的&#xff0c;有几个小朋友问过几次需要在哪改&#xff0c;写出来不会的参考下。 1、新建一个“.htaccess”文件 2、复制以下代码&#xff1a;&#xff08;a你的域名&#xff09; Options FollowSymLinks rewrite…

Vue 从入门到进阶之路(十四)

之前的文章我们对 vue 的基础用法已经有了很直观的认识&#xff0c;本章我们来看一下 vue 中的生命周期函数。 上图为 Vue官方为我们提供的完整的生命周期函数的流程图&#xff0c;下面的案例我们只是走了部分情况流程&#xff0c;但所有的生命周期函数都涉及到了。 1 <!DOC…

css 清除浮动float 嗒嘀嗒滴 ----20181120

手动给父元素添加高度通过clear清除内部和外部浮动给父元素添加overfloat属性并结合zoom:1使用给父元素添加浮动        常用方法&#xff1a; ①给兄弟元素添加clear属性&#xff1a;添加一个class为clear&#xff1a;both&#xff0c; ②添加伪类&#xff1a;.clearf…

applet实现大文件ftp上传(一)

由于要用APPLET实现大文件FTP上传下载&#xff0c;从网上搜索了几下&#xff0c;找到很多资料&#xff0c;最后决定采用基于 org.apache.commons.net.ftp包实现FTP上传下载&#xff0c;Net包中的类既提供对协议的底层访问也有高层的抽象。在大多数情况下&#xff0c;抽 象是足够…

微软最爽命令行工具发布!

最近微软发布了命令行工具Windows Terminal&#xff0c;简直要让我们爽翻天 长期以来&#xff0c;Linux和Mac下的命令行工具自成一派&#xff0c;Windows却是另一套命令系统。这就比较恶心了&#xff0c;这也是很多开发者不选择Windows系统的原因。 但是现在完全不一样了&#…

定时任务 Cron表达式

Cron表达式由6~7项组成&#xff0c;中间用空格分开。从左到右依次是&#xff1a; 秒、分、时、日、月、周几、年&#xff08;可省略&#xff09; Cron表达式的值可以是数字&#xff0c;也可以是以下符号&#xff1a; "*"&#xff1a;所有值都匹配 "?"&…

vue项目中 axios请求拦截器与取消pending请求功能 - 年少、 - 博客园

在开发vue项目中&#xff0c;请求是不可缺少的&#xff0c;在发送请求时常常需要统一处理一些请求头参数等设置与响应事件&#xff0c;这时利用请求拦截器再好不过。 这里以axios请求为例 实现了设置统一请求头添加token, 其中token在登录时被存入了localStorage中。 同时拦…

gitbook 入门教程之解决windows热加载失败问题

破镜如何贴花黄 gitbook 在 Windows 系统无法热加载,总是报错! gitbook 是一款文档编写利器,可以方便地 markdown 输出成美观优雅的 html ,gitbook serve 启动服务器后,原来相貌平平的 markdown 丑小鸭摇身一变就成了倾国倾城的 html 绝色佳人. 如果源文件发生更改,Windows 却无…

理解DOM事件流的三个阶段 - Lxxyx的开发笔记 - SegmentFault 思否

本文主要解决两个问题&#xff1a; 什么是事件流 DOM事件流的三个阶段 起因 在学习前端的大半年来&#xff0c;对DOM事件了解甚少。一般也只是用用onclick来绑定个点击事件。在寒假深入学习JavaScript时&#xff0c;愈发觉得自己对DOM事件了解不够&#xff0c;遂打开我的《J…

支付宝APP支付 统一下单 php服务端 tp5

/*支付宝第三方支付 * *生成APP支付订单信息 * param number  uid 用户id * param string   token 用户token * param number  oid 订单id * param string   title 标题 * param string    body …

python第十九天(random、json、pickle、hashlib、hmac、shutil、shevle模块)

今日内容&#xff1a; 1. random 模块 2. json模块 3. pickle 模块 4.hashlib 模块 5. hmac 模块 6. shutil 模块 7. shelve 模块 1. random 模块&#xff1a; random 模块 获取随机值import randomfor i in range(10): print(random.random()) # random.random() 随机获取…

NodeJS入门04-Express路由和中间件 - 小之 - 博客园

nodeJS入门04-Express路由和中间件 Express框架是后台的Node框架&#xff0c;在后台的受欢迎的程度&#xff0c;和jQuery一样&#xff0c;就是企业的事实上的标准。 路由 路由是指如何定义应用的端点&#xff08;URIs&#xff09;以及如何响应客户端的请求。 路由是由一个 …

jmeter(四十五)常用Beanshell脚本

整理了一批jmeter常用的beanshell脚本供大家参考&#xff01; 时间戳 import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; try{ Date date new Date(); //获取当前时间 SimpleDateFormat sf new SimpleDateFormat("yyyy-MM-dd HH:mm…

Critical error detected c0000374

我发现出现上述错误是 free 两次内存 float* ddnew float[2];delete[] dd;delete[] dd;转载于:https://www.cnblogs.com/hook-gou/p/9994662.html

nodejs开发 过程中express路由与中间件的理解 - pyj063 - 博客园

nodejs开发 过程中express路由与中间件的理解 nodejs开发 express路由与中间件 路由 通常HTTP URL的格式是这样的&#xff1a; http://host[:port][path] http表示协议。 host表示主机。 port为端口&#xff0c;可选字段&#xff0c;不提供时默认为80。 path指定请求资源的…

错误MSB4018 “ResolvePackageAssets”任务意外失败的解决方法

昨天系统奔溃了&#xff0c;重装系统后发现&#xff0c;之前写的.netcore项目打开后重新生成报错&#xff0c;错误如下严重性 代码 说明 项目 文件 行 禁止显示状态 错误 MSB4018 “ResolvePackageAssets”任务意外失败。 NuGet.Packaging.Core.Packag…