1

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();
    }
}
2
  • please give preview of your code so we can check what can you do in laravel way ? Commented Sep 22, 2016 at 13:32
  • If you return an associative array you can then call a view via return view('greeting', $functionResult)->with('moreFields',...); Commented Sep 22, 2016 at 13:34

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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!

4 Comments

public function index() { return view("index")->with(["total" => $this->calculateTotal($a, $b)]); }
@jakubwrona Yeah, that would work too; just depends if you want to do anything with $total before returning to the view :P All depends on context
Do I not need to extend the index() function or because the variables are passed in next to the function name () their globally accessible.
You can either pass in variables like $a, or define properties of the class, like public $a = 1; and access it like $this->a, which would be a "global" variable (for that class anyway). You may want to look into standard object oriented PHP; will give you more insight to what's happening here
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.