archvist
archvist

Reputation: 722

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. 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.

Does laravel recommend any standards for this? or can I create a function within a controller and just pass the final variables back as I would in 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.

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();
    }
}

Upvotes: 1

Views: 5271

Answers (3)

WindSaber
WindSaber

Reputation: 323

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.

I hardly recommend to create helpers classes under \App\Helpers

Returning to your example I would create a class in \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.

Upvotes: 3

jakub wrona
jakub wrona

Reputation: 2254

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

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

in your partial methods calculating data.

Upvotes: 0

Tim Lewis
Tim Lewis

Reputation: 29326

Controllers are a PHP class, so you can use functions within them the same as you would any other class. 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.

Hope that helps!

Upvotes: 2

Related Questions