Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirection in laravel without return statement

Tags:

php

laravel

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.

like image 567
anwerj Avatar asked Aug 30 '14 10:08

anwerj


People also ask

How do I redirect from one page to another in Laravel?

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.

Can we send data with redirect in Laravel?

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').

How do I redirect back in Laravel?

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();


2 Answers

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('/') }}
like image 128
tacone Avatar answered Sep 19 '22 12:09

tacone


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');
 }
like image 35
Laurence Avatar answered Sep 19 '22 12:09

Laurence