简体   繁体   English

在Laravel控制器中使用函数

[英]Using functions within a Laravel controller

I have a standard laravel controller function with over 350 lines of logic for a number of different on-page elements. 我有一个标准的laravel控制器功能,具有350多个逻辑行,可用于许多不同的页面上元素。 Some of the logic could be taken out put within it's own function but I would need to pass the variables back to the laravel controller to be used within a view. 某些逻辑可以放在其自身的函数中,但我需要将变量传递回laravel控制器以在视图中使用。

Does laravel recommend any standards for this? laravel是否为此建议任何标准? or can I create a function within a controller and just pass the final variables back as I would in php? 或者我可以在控制器内创建一个函数,然后像在php中那样将最终变量传递回来?

Example of current controller, I would like to split logic from here into its own function and return the values from the new function back to this getIndex() function. 以当前控制器为例,我想将逻辑从这里拆分成自己的函数,并将新函数中的值返回给此getIndex()函数。

class PubsController extends Controller
{
    public function getIndex()
    {
        //Date Helpers
        $dateThisMonth = Carbon::now()->startOfMonth()->toDateString();
        $dateLastMonth = Carbon::now()->subMonth()->startOfMonth()->toDateString();
        $dateNextMonth = Carbon::now()->addMonth()->startOfMonth()->toDateString();
    }
}

Even there's not an standard about how big a controller can be, remember that the php class size recomendation is not to exced 200 lines. 即使没有关于控制器有多大的标准,也请记住,php类的大小建议不要超过200行。

I hardly recommend to create helpers classes under \\App\\Helpers 我几乎不建议在\\ App \\ Helpers下创建帮助器类。

Returning to your example I would create a class in \\App\\Helpers\\DateHelper.php 回到您的示例,我将在\\ App \\ Helpers \\ DateHelper.php中创建一个类

<?php

namespace App\Helpers;

use Carbon\Carbon;

class DateHelper {
   public static getDateThisMonth(){
       return Carbon::now()->startOfMonth()->toDateString();
   }
   public static getDateLastMonth(){
       return Carbon::now()->subMonth()->startOfMonth()->toDateString();
   }
   public static getDateNextMonth(){
       return = Carbon::now()->addMonth()->startOfMonth()->toDateString();
   }
}

And then you can call in your controller something like: 然后,您可以在控制器中调用类似以下内容的代码:

use App\Helpers\DateHelper;
class PubsController extends Controller{

 public function getIndex()
    {
        $dateThisMonth = DateHelper::getDateThisMonth();
        $dateLastMonth = DateHelper::getDateLastMonth();
        $dateNextMonth = DateHelper::getDateNextMonth();
    }
}

In some cases is also recommended to move some logic to your model instead of your controller. 在某些情况下,还建议将某些逻辑移至模型而不是控制器。

Controllers are a PHP class, so you can use functions within them the same as you would any other class. 控制器是一个PHP类,因此您可以像使用其他任何类一样在其中使用函数。 For example, if you have a line of logic that calculates a total before passing it to your index view, you could do something like this: 例如,如果您有一行逻辑在将总计传递到索引视图之前先计算总计,则可以执行以下操作:

public function index(){ 
    $total = $this->calculateTotal($a, $b);
    return view("index")->with(["total" => $total]);
}

public function calculateTotal($a, $b){
    return $a + $b;
}

$this within a controller can access properties or functions within that controller, so you're free to define any helper functions and access them accordingly. 控制器内的$this可以访问该控制器内的属性或函数,因此您可以自由定义任何辅助函数并进行相应的访问。

Hope that helps! 希望有帮助!

There are several ways to reorganize your code. 有几种方法可以重新组织代码。 You can have the methods returning something and then assign all of the results like @tim-lewis shown but also you may use 您可以让方法返回某些内容,然后分配所有结果,例如显示的@ tim-lewis,但也可以使用

View::share('someName', $someValue); 

in your partial methods calculating data. 在部分方法中计算数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM