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.pswp.cn/news/248997.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/248997.shtml
英文地址,請注明出處:http://en.pswp.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…