Created
July 22, 2012 09:35
-
-
Save shakyShane/3159068 to your computer and use it in GitHub Desktop.
Handle page requests in laravel controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* 1. Check if it's an ajax request | |
* 2. Set up Page Data (title, activenav etc) | |
* 3. add any addtional data to be passed to the View. | |
* @param $route | |
* @return mixed | |
* | |
* | |
*/ | |
public function servePage($route, $extraViewData = null) | |
{ | |
if (\Laravel\Request::ajax()) | |
$this->layout = 'layouts.ajax'; | |
else | |
$this->layout = 'layouts.main'; | |
/** | |
* Helper to set the Page title and ACTIVE nav element | |
*/ | |
$viewData = $this->setPageData($route); | |
/** | |
* | |
* Handle any extra data that needs to be passed to the view. | |
* If it's an Array, just merge it. | |
* If it's an object, make it accessible via it's class name. | |
*/ | |
if (null !== $extraViewData){ | |
if (is_array($extraViewData)){ | |
$viewData = array_merge($extraViewData, $viewData); | |
} | |
if (is_object($extraViewData)){ | |
$className = strtolower(get_class($extraViewData)); | |
$viewData[$className] = $extraViewData; | |
} | |
} | |
return View::make($this->layout, $viewData)->nest('content', 'home.' . $route, $viewData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment