Laravel Course

- Redirecting to Controller Actions
- Redirecting with Flashed Session data
- Redirect in Constructor

There are several ways to redirect responses. The simplest method is to use the global redirect() helper.
Route::get('noprofile', function(){
  return redirect()->to('user/profile');
});
Or:
Route::get('noprofile', function(){
  return redirect('user/profile');
});
There is also a back() function to redirect the user to their previous location.
Route::post('user/profile', function(){
  // Validate the request...

  return back()->withInput();
});

Redirecting To Named Routes

Each Route can have a specific name. The name can be assigned using the name() method.
- Example:
1. In routes/web.php create a route for "/test" page, with the name "testing".
Create another route for "/redirect" which will redirect the request to the named route "testing".
Route::name('testing')->get('/test', function(){
   return 'Life is an effect, the cause is Love.';
});

Route::get('redirect',function(){
   return redirect()->route('testing');
});
2. Visit the following URL to test the named route example.
//localhost:8000/redirect
After execution of the above URL, you will be redirected to //localhost:8000/test , the location of the named route "testing".

If your route has parameters, you may pass them into an array as the second argument to the route() method.
// For a route with the following URI: profile/{id}

return redirect()->route('profile', ['id'=>1]);

Redirecting to Controller Actions

To redirect to controller actions, pass the controller and the method name to the action() function.
return redirect()->action('HomeController@index');
If your controller route requires parameters, you may pass them into an array as the second argument to the action() method:
return redirect()->action('UserController@profile', ['id'=>1]);

Redirecting with Flashed Session data

If you want to pass a message to the page where the user is redirected, for example after successfully performing an action, you may create a redirect response and flash data to the session using the with() method.
Route::post('user/profile', function(){
  // Update the user's profile...

  return redirect('dashboard')->with('status', 'Profile updated!');
});
After the user is redirected, you may display the flashed message from the session. For example, using Blade syntax:
@if(session('status'))
 <div class="alert-success">
  {{ session('status') }}
 </div>
@endif
After redirect with the with() method, the flashed session is automatically deleted from "session".

Redirect in Constructor

To make a redirect() from a __construct() of a controller, to make sure that the redirect() is performed, without executing other method, use: redirect()->send().
If you want to make a redirect in __construct() according to a session, use the $this->middleware() method in constructor, with the code for redirect in a callback function, like in this syntax.
public function __construct(){
  $this->middleware(function($request, $next){
    //Your code with Session, for redirect

    return $next($request); //<- this is required
  });
}

- Example, a piece of code from a controller. If the Session 'admin_logg' not exist, it will redirect to '/'; the test() method will not be executed:
public function __construct(Request $request){
  //this Closure is needed because it is used session in __construct
  $this->middleware(function($request, $next){
    if(!session()->has('admin_logg')){
      return redirect('/')->send();
    }
    return $next($request);
  });

  $this->test();
}

public function test(){
  echo 'Laravel tutorial';
}

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Redirects

Last accessed pages

  1. CSS3 2D transforms (868)
  2. Create and Use in PHP a Simple Template Page (3306)
  3. Common PHP Errors and Solutions (9771)
  4. PHP getElementById and getElementsByTagName (49471)
  5. Mysql SELECT JOIN tables on two different Databases (4498)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)