Redirect Using PHP Method Chaining?

Hamed Qaderi

Let's say we want to redirect a user to a form page with some errors after submitting. I have this method in my controller.

return redirect('create')->withErrors($errors)->withInputs($inputs);

Now I have a helper function named redirect which returns a new Redirect object.

function redirect($fileName)
{
 return new Redirect($fileName);
}

and here is the sturcture of Redirect class

class Redirect
{

  public function __construct($fileName)
  {
    header("Location: " . $fileName);

    return $this;
  }


  public function withErros($errors)
  {
    session_start();
    $_SESSION['errors'] = $errors;

    return $this;
  }



  public function withInputs($inputs)
  {
    session_start();
    $_SESSION('inputs') = $inputs;

    return $this;
  }
}

The first chaining method (withErrors) runs correctly and I can show errors in my view. But the second one (withInputs) never runs. I think it's because of header function in PHP. Can somebody help me, please?

JOUM
class Redirect
{
  protected $filename;
  public function __construct($fileName)
  {
      session_start();
      $this->filename=$filename;
  }
  public function __destruct()
  {
      $this->redirect();
  }
  public function withErros($errors)
  {
      $_SESSION['errors'] = $errors;
      return $this;
  }
  public function withInputs($inputs)
  {
      $_SESSION('inputs') = $inputs;
      return $this;
  }
  public function redirect(){
      header("Location: " . $this->filename);
      exit;
  }
}

This can be a solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel redirect() doesn't work on chaining method

From Dev

PHP - Method Chaining - Best Approach?

From Dev

PHP Method chaining with dynamic names

From Dev

Check if call is method chaining PHP

From Dev

Method chaining using javascript not working?

From Dev

Method chaining using javascript not working?

From Dev

Chaining method calls using RxJava

From Dev

@return annotation for a chaining enabling method in PHP

From Dev

redirect using header in php

From Dev

PHP: get a result from a chaining method from extended classes?

From Dev

When chaining methods in PHP, Is it possible to determine which method was called first?

From Dev

use Variable as methods in PHP chain to allow conditional method chaining

From Dev

Method chaining with the same method

From Java

How to use groupby transform across columns using method chaining?

From Dev

how to do logging of method chaining using spring aop

From Dev

how to do logging of method chaining using spring aop

From Dev

Passing 'this' context within method chaining (using apply / call or bind)

From Dev

Redirect to php script using htaccess

From Dev

Using _top redirect from PHP

From Dev

Redirect to a POST Route using PHP

From Dev

Login page redirect using php

From Dev

PHP Header Redirect Using Variable

From Dev

Swift - Method chaining

From Dev

Method chaining in ruby

From Dev

Return type for method chaining

From Dev

Python class method chaining

From Dev

Method Chaining and Class Inheritance

From Dev

Method chaining with asyncio coroutines

From Dev

Method Chaining based on condition