【问题标题】:Creating a helper system in laravel is correct or not?在 laravel 中创建辅助系统是否正确?
【发布时间】:2018-02-28 16:37:30
【问题描述】:

我的助手根 app\http\myHelpers\customClass.php

customClass.php

<?php

namespace App\Http\myHelper;

class CustomClass {

    public static function customFunction(){
        return 'Custom class working......';
    }
}

控制器功能

public function test(){
        CustomClass::customFunction();
    }

路线

Route::get('/test', 'HomeController@test');

不需要 composer 命令。它工作正常,但我不确定它是正确的系统还是错误的系统。请帮帮我。

【问题讨论】:

    标签: laravel class controller routes helpers


    【解决方案1】:

    您如何组织代码确实是个人选择。所以你的代码没有问题。你不需要任何 composer 命令,因为在 Laravel 中 app 文件夹中的所有内容都是由 composer 自动加载的:The App Directory

    【讨论】:

      【解决方案2】:

      这很好,您还可以做的是在特征内部组织函数并将它们放置例如在 /app 文件夹中。

      <?php
      
      namespace App;
      
      trait HasRoles
      {
        public function hasPermission(Permission $permission)
          {
              return $this->hasRole($permission->roles);
          }
      }
      

      并在你的控制器中使用这个特性

      use Authenticatable, Authorizable, CanResetPassword, HasRoles;
      

      只是捆绑辅助函数的另一种方式!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-27
        • 2016-09-17
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 2015-12-01
        • 2016-07-31
        • 1970-01-01
        相关资源
        最近更新 更多