Skip to content

Instantly share code, notes, and snippets.

@shakyShane
Created July 22, 2012 09:35
Show Gist options
  • Save shakyShane/3159068 to your computer and use it in GitHub Desktop.
Save shakyShane/3159068 to your computer and use it in GitHub Desktop.
Handle page requests in laravel controller
/**
*
* 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