日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

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

發布時間:2023/12/13 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 namespace App\Http\Controllers; class PagesController extends Controller { ??/** ?? * Display homepage. ?? * ?? * @return Response ?? */ ??public function getHome() ??{ ????return view('pages.home'); ??} }

?

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:

?

1 2 3 4 5 6 7 class PricesClass { ??public function getPrices() { ????return ['bronze' => 50, 'silver' => 100, 'gold' => 150]; ??} }

?

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:

?

1 2 3 4 5 6 7 8 <?php namespace App\Classes; class PricesClass { // ...

?

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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 namespace App\Http\Controllers; use App\Classes\PricesClass; class PagesController extends Controller { ??/** ?? * Display homepage. ?? * ?? * @return Response ?? */ ??public function getHome() ??{ ????$pricesClass = new PricesClass(); ????$prices = $pricesClass->getPrices(); ????return view('pages.home', compact('prices')); ??} }

?

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:

?

1 2 3 4 5 6 7 <?php function getPrices() { ??return ['bronze' => 50, 'silver' => 100, 'gold' => 150]; }

?

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:

?

1 2 3 4 5 6 7 public function getHome() { ??include(app_path() . '\functions\prices.php'); ??$prices = getPrices(); ??// ...

?

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)

?

1 2 3 4 5 6 7 8 9 10 11 12 13 ????"autoload": { ????????"classmap": [ ????????????"database" ????????], ????????"psr-4": { ????????????"App\\": "app/" ????????}, ????????"files": [ ????????????"app/functions/prices.php" ????????] ????},

?

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”:

?

1 2 3 4 5 6 7 8 ????"autoload": { ????????"classmap": [ ????????????"database", ????????????"app/functions" ????????], ????},

?

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

總結

以上是生活随笔為你收集整理的How to use external classes and PHP files in Laravel Controller?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。