Thursday, February 1, 2018

Is it O’K to add class properties and constructor to Laravel Controller class? - development

Is it O’K to add class properties and constructor to Laravel Controller class?

I want to perform my PHP/Laravel code, so to make it easier in support I tried to add class properties to Controller. Everything works, but I want to know is it acceptable trick from the view point of Laravel framework.

class TopPageController extends Controller {

    private $request;
    private $pageHtmlHeadData;

    public function __construct() {
        $this -> pageHtmlHeadData =  $this -> getTopPageHtmlHeadData();
    }


    public function renderTopPage(Request $request){
        $this -> request = $request;
        dd('Enougn for now');
    }

    private function getTopPageHtmlHeadData(){
       retrun // SQL request ...
    }
}

As you see, in the code above I did two unusual things: setup the $this -> request instance when renderTopPage() was called from the web.php (now we can access to if from any method of the class) and perform sql request when the instance of TopPageController has been created (really I don’t know when it was).



from Laravel Questions and Answers https://laravelquestions.com/php/is-it-ok-to-add-class-properties-and-constructor-to-laravel-controller-class/
via Lzo Media

No comments:

Post a Comment