Skip to content

Instantly share code, notes, and snippets.

@gravitano
Created March 27, 2014 14:00
Show Gist options
  • Save gravitano/9808228 to your computer and use it in GitHub Desktop.
Save gravitano/9808228 to your computer and use it in GitHub Desktop.
Simple trick for your BaseController.php
<?php
class BaseController extends Controller {
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
/**
* Get the evaluated view contents for the given view.
*
* @param string $view
* @param array $data
* @param array $mergeData
* @return \Illuminate\View\View
*/
public function view($name, $data = array(), $mergeData = array())
{
return View::make($name, $data, $mergeData);
}
/**
* Create a new redirect response to the given path.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function redirect($path, $status = 302, $headers = array(), $secure = null)
{
return Redirect::to($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to a named route.
*
* @param string $route
* @param array $parameters
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectRoute($route, $parameters = array(), $status = 302, $headers = array())
{
return Redirect::route($route, $parameters, $status, $headers);
}
/**
* Create a new redirect response to the previous location.
*
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectBack($status = 302, $headers = array())
{
return Redirect::back($status, $headers);
}
/**
* Create a new redirect response to the "home" route.
*
* @param int $status
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectHome($status = 302)
{
return Redirect::home($status);
}
/**
* Create a new redirect response to the previous location.
*
* @param int $status
* @param array $headers
* @return \Illuminate\Http\RedirectResponse
*/
public function goBack($status = 302, $headers = array())
{
return $this->redirectBack($status, $headers);
}
/**
* Create a new redirect response to the "home" route.
*
* @param int $status
* @return \Illuminate\Http\RedirectResponse
*/
public function goHome($status)
{
return $this->redirectHome($status);
}
}
@gravitano
Copy link
Author

Contoh Penggunaan :

Alternatif kode untuk menampilkan view :

return $this->view('home');

Alternatif kode untuk meredirect ke halaman (uri) tertentu :

return $this->redirect('sessions/create');

Untuk method lainnya, cara pemanggilannya sama seperti memanggil method dari class tertentu pada umumnya.
Tinggal diatur sesuai kebutuhan saja :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment