zoukankan      html  css  js  c++  java
  • How to use external classes and PHP files in Laravel Controller?

     

    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 AppLibraries, AppClasses or AppServices. Or you can call it your own application – AppMyApp. This is totally your choice.

    So, in this example, let’s save the class as AppClassesPricesClass.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 
  • 相关阅读:
    c++ 网络编程(四) LINUX/windows下 socket 基于I/O复用的服务器端代码 解决多进程服务端创建进程资源浪费问题
    c++ 网络编程(三) LINUX/windows 进程间的通信原理与实现代码 基于多进程的服务端实现
    c++ 网络编程(二) linux 下多进程socket通信 多个客户端与单个服务端交互代码实现回声服务器
    c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码
    c++ MFC图像处理CImage类常用操作代码
    vue 模板语法
    占位1
    MongoDB
    Node.js fs-文件系统
    nodeJs 常用模块(一)
  • 原文地址:https://www.cnblogs.com/mouseleo/p/9979418.html
Copyright © 2011-2022 走看看