Php Laravel:如何启动全球图书馆

Php Laravel:如何启动全球图书馆,php,laravel,Php,Laravel,我有 app/Libraries/Cart.php app/Libraries/Currency.php app/Libraries/SomeOther1.php app/Libraries/SomeOther2.php 在我所有的控制器中,我不想使用和声明。 我不想要这个: <?php namespace App\PathToFolder; use App\Libraries\Cart; use App\Libraries\Currency; class SomeControlle

我有 app/Libraries/Cart.php app/Libraries/Currency.php app/Libraries/SomeOther1.php app/Libraries/SomeOther2.php

在我所有的控制器中,我不想使用和声明。 我不想要这个:

<?php
namespace App\PathToFolder;

use App\Libraries\Cart;
use App\Libraries\Currency;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart = new Cart;
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);

    $this->currency = new Currency;
    $this->currency->format(...);
  }

你可以不使用下面的代码。改为在实例化中添加名称空间

<?php
namespace App\PathToFolder;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart = new \App\Libraries\Cart;
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);

    $this->currency = new \App\Libraries\Currency;
    $this->currency->format(...);
  }

通常我会在不需要在控制器或基本控制器中加载大量init函数的情况下使用helper类

我创建了新的文件夹callhelpers,并创建了新的类名Helpers.php

在composer.json中的“自动加载”下,将助手类添加到其中

"autoload": {
    ...
    "files": ["app/Helpers/helpers.php"]
},
运行编写器转储自动加载

在helper.php类中。。见下面的示例

if (! function_exists('get_cart')) {

    function get_cart()
    {
       return new Cart();
    }
}
用法:

获取购物车->添加。。。; 获取购物车->删除

如果您不想使用helper类。只做静态类

用法:

购物车::添加。。。 购物车::删除


使用Trait,但您仍然在$this->cart->add中使用$this。下面还有一种方法可以实现此目的,然后使用其他人提到的Trait或composer文件

您可以在父控制器中声明和初始化变量并使用它

比如说,

根据您的代码,您的父控制器a

<?php

 namespace App\Http\Controllers;

 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 use App\Libraries\Cart;
 use App\Libraries\Currency;

 class Controller extends BaseController
 {
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    private $cart;
    private $currency;

    public function __construct()
    {
        //blockio init
        $this->cart = new Cart;
        $this->currency = new Currency; 
    }

 }
现在你可以像这样在你的控制器中使用这个函数

<?php
namespace App\PathToFolder;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);
    $this->currency->format(...);
  }

你可以使用traits,但在traits中,你必须添加use…,让我知道你是否需要它,我可以指导你如何使用它。在使用存储库函数时,你必须使用一个“use”
<?php

 namespace App\Http\Controllers;

 use Illuminate\Foundation\Bus\DispatchesJobs;
 use Illuminate\Routing\Controller as BaseController;
 use Illuminate\Foundation\Validation\ValidatesRequests;
 use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
 use App\Libraries\Cart;
 use App\Libraries\Currency;

 class Controller extends BaseController
 {
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    private $cart;
    private $currency;

    public function __construct()
    {
        //blockio init
        $this->cart = new Cart;
        $this->currency = new Currency; 
    }

 }
<?php
namespace App\PathToFolder;

class SomeController extends Controller
{
  public someFunction()
  {
    $this->cart->add(...);
    $this->cart->remove(...);
    $this->cart->clear(...);
    $this->currency->format(...);
  }