i have this blogsController, the create function is as follows.
public function create() {
  if($this->reqLogin()) return $this->reqLogin();
  return View::make('blogs.create');
 }
In BaseController, i have this function which checks if user is logged in.
    public function reqLogin(){
      if(!Auth::check()){
        Session::flash('message', 'You need to login');
        return Redirect::to("login");
      }
    }
This code is working fine , but it is not what is need i want my create function as follows.
public function create() {
  $this->reqLogin();
  return View::make('blogs.create');
 }
Can i do so?
Apart from that, can i set authantication rules , like we do in Yii framework, at the top of controller.
By default laravel will redirect a user to the home page like so: protected $redirectTo = '/home'; To change that default behaviour, add the following code in App/Http/Controllers/Auth/LoginController. php . This will redirect users (after login) to where ever you want.
Redirect with Data Firstly, you can just use with(): return redirect()->back()->with('error', 'Something went wrong. '); This code will add an item to the Session Flash Data, with key "error" and value "Something went wrong" - and then you can use that in the result Controller or View as session('error').
If you just want to redirect a user back to the previous page (the most common example - is to redirect back to the form page after data validation failed), you can use this: return redirect()->back();
Beside organizing your code to fit better Laravel's architecture, there's a little trick you can use when returning a response is not possible and a redirect is absolutely needed.
The trick is to call \App::abort() and pass the approriate code and headers. This will work in most of the circumstances (excluding, notably, blade views and __toString() methods.
Here's a simple function that will work everywhere, no matter what, while still keeping your shutdown logic intact.
/**
 * Redirect the user no matter what. No need to use a return
 * statement. Also avoids the trap put in place by the Blade Compiler.
 *
 * @param string $url
 * @param int $code http code for the redirect (should be 302 or 301)
 */
function redirect_now($url, $code = 302)
{
    try {
        \App::abort($code, '', ['Location' => $url]);
    } catch (\Exception $exception) {
        // the blade compiler catches exceptions and rethrows them
        // as ErrorExceptions :(
        //
        // also the __toString() magic method cannot throw exceptions
        // in that case also we need to manually call the exception
        // handler
        $previousErrorHandler = set_exception_handler(function () {
        });
        restore_error_handler();
        call_user_func($previousErrorHandler, $exception);
        die;
    }
}
Usage in PHP:
redirect_now('/');
Usage in Blade:
{{ redirect_now('/') }}
                        You should put the check into a filter, then only let the user get to the controller if they are logged in in the first place.
Filter
Route::filter('auth', function($route, $request, $response)
{
    if(!Auth::check()) {
       Session::flash('message', 'You need to login');
       return Redirect::to("login");
    }
});
Route
Route::get('blogs/create', array('before' => 'auth', 'uses' => 'BlogsController@create'));
Controller
public function create() {
  return View::make('blogs.create');
 }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With