Support the ongoing development of Laravel.io →
Architecture
Last updated 3 months ago.
0

It slows it down marginally because you're loading a lot of code into memory that you don't need on every request, however this is relatively negligible compared to the weight of the rest of the framework.

You shouldn't give each page its own controller unless that page has multiple functions (showing a form, posting to a form etc). If all you have is a controller with a bunch of static views, you could probably write an abstract method that takes a page as a parameter to save yourself a lot of typing:

e.g.

Route::get(/{page}, '[email protected]');

then in SiteController

public function page($page)
{
      $data = $query->("select from $page ....");
      return View::make($page, compact($data));
 }

You'd have to establish some kind of naming convention for that to work though.

But if you have a lot of pages with more complex dynamic data, then you might want to consider splitting up the controller into logical chunks. Keep related things together.

Also, if you plan on putting this under version control to have multiple people working on it, you'll definitely want to split up your controller into lots of very focused controllers so as to avoid potential commit coflicts.

But regarding controller size, the quantity of controller functions isn't quite as important as how heavy each function is. Controller methods should be EXTREMELY slim. Almost no if statements or logic, just very simple imperative commands to get only the data needed to render the page.

5 very fat controller methods is worse than 20 very thin controller methods.

Last updated 3 months ago.
0

I'm not sure how big is too big in Laravel's term, however, I would consider keeping the controllers light and let the models do the heavy (data) lifting.

The model does not necessary have to map to a specific table for what is worth, you may call it a model utility class with static helper methods to access the data.

Last updated 3 months ago.
0

Thanks for your responses. I have read about fat model and skinny controller. Issue I have is understanding how to such queries within my models. I want to utilise eloquent ORM.

Below is an example of what I am running with one function in a controller:

public function getRetailers($city) {    

	 $locations = DB::table('retailers_listings')
    	->orderBy('country', 'asc')
       	->Where('city', $city)
       	->get();
    	  
       	$this->layout->title = 'Stores - ' . $city .'';
       	$this->layout->title_city = $city;
       	$this->layout->description = ' ' . $city .'  stores in ' . $city .' ';
   		$content = View::make('retailers.stores')
       	->with('store_listings', $locations)
       	->with('retailers_listings', $this->pagesElements->RetailersNavigation())
       	->with('title', $this->layout->title)
       	->with('title_city', $this->layout->title_city)
		->with('description', $this->layout->description);

       	if (Request::header('X-PJAX')) {
        return $content;
       	} else { 
       	 	$this->layout->content = $content; 
       	} 
    }

Now I'm using pjax within this particular controller, however should this query be moved into a model or using eloquent and is it correct practice? I find there is not much documentation on how to structure above queries etc within the model.

Last updated 3 months ago.
0

It seems like all the logic of creating page titles, rendering navigation, etc should belong in the view and not your controllers. A controller doesn't have any business determining the title of the page it's rendering, the page being rendered should be passing it's title to the layout via @section or similar mechanic.

Last updated 3 months ago.
0

I would move the following to its own model and call that model to supply the data to the controller:


     $locations = DB::table('retailers_listings')
        ->orderBy('country', 'asc')
        ->Where('city', $city)
        ->get();

You may want to read the models doc for more ideas at:

http://laravel.com/docs/eloquent

Last updated 3 months ago.
0

jgarifuna said:

I would move the following to its own model and call that model to supply the data to the controller:


    $locations = DB::table('retailers_listings')
       ->orderBy('country', 'asc')
       ->Where('city', $city)
       ->get();

You may want to read the models doc for more ideas at:

http://laravel.com/docs/eloquent

This is my next step, but I cannot seem to work out how to run this in the model and then call it from my controller with the variable $city. If you could give me an example, that would be a lot easier for me to understand. The issue i have with Laravel docs is it is sort of limited. I have been over the elequent documentation but cant seem to make the above work with the model, as there is no clear example for the above.

Last updated 3 months ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

Panoplr Panoplr panoplr Joined 27 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.