What's the difference between 'withRedirect' versus 'view->render'

Hello,
I’ve seen two different ways to redirect a page after a validation form authentification :

  1. return $response->withRedirect($this->router->pathFor(‘home’, $params));
  2. return $this->view->render($response, ‘/pages/home.twig’, $params);

I don’t exactly understand the difference, but I don’t retrieve the same vars.
First, I’ve followed the 1st method in a ‘Codecourse’ tutorial on youtube. Then I’ve tried to develop Respect-Validation with different languages with the help of this github which basically use the second method with “$this->view->render”

This method helps me to translate my error-form-validation (by getting the vars from languages I load in the controller), but I couldn’t get the vars from the container that is set like that :

$container[‘view’] = function($container){
$view->getEnvironment()->addGlobal(‘auth’, [
‘check’ => $container->auth->check(), // si connecté ou non
‘user’ => $container->auth->user() // id utilisateur
]);
return $view;
};

…And I can’t see in my view if I’m connected or not with “$this->view->render” (I have to reload the page once). If I use “$response->withRedirect”, I don’t get my language datas… :-/
How can I get the advantage of these two redirections methods or how can I change my settings to get all the vars ?

Thank you very much for any help !

I still don’t understand the problem. Anyone to help ?

  1. withRedirect will send a HTTP 302 redirect response header to the client (browser). The browser will then redirect the user to the given url. The redirection will happen on the client side (browser).

  2. render just renders the twig template with the given view data and returns a response object. The Browser receives just a standard html page. The response (result) will be displayed as normal page in the browser (client side).

I would not mix templates with business logic like this. Better pass all the required view data in your controller action and not within the container.

Ok I think I understood what you said on points 1) and 2), but to be sure… So, you would :

  1. make your php controls in the authcontroller…
  2. maybe store the vars in $_SESSION if need for possibly different steps in different controllers
  3. pass array vars for the view though params with : $this->view->render($response, $file, $params);
  4. In twig file, display the view according my params

Is this logic correct ?

  1. Not sure what you mean. Without code examples it’s hard to understand. Maybe take a look at the “Slim Validation” AuthController.php
  2. This depends on the specific use case. The session is often used for flash messages in this context (validation).
  3. yes
  4. yes

Yes. thank you ! For 1), I’ve effectively used Respect/Validation from the page you said.

For 2), I would use session for flash message. But also for the data’s user : When the user signs in, this would be store in session vars ? (for exemple his user-id)

CodeCourse did it completely differently. They make an Auth Class like this :

  class Auth{
  	public function user(){ 
  		return User::find($_SESSION['user']);
  	}
  	public function check(){
  		return isset($_SESSION['user']);
  	}

  	public function attempt($email, $password){
  		$user = User::where('email', $email)->first();
  		if(!$user){return false;}
  		if(password_verify($password, $user->password)){
  		$_SESSION['user'] = $user->id;
  		return true;
  		}
  		else{return false;}
  	}
  	public function logout(){ unset($_SESSION['user']);}
  }

And in the container, they transmit auth vars as global vars :

	$container['view'] = function($container){
		$view = new \Slim\Views\Twig( __DIR__ . '/../app/Views', ['cache' => false]);
		$view->addExtension(new \Slim\Views\TwigExtension( $container->router, $container->request->getUri()));
		$view->getEnvironment()->addGlobal('auth', [
			'check' => $container->auth->check(), // si connecté ou non
			'user' => $container->auth->user() // id utilisateur
		]);
		$view->getEnvironment()->addGlobal('flash', $container->flash);
		return $view;
	}; 
  1. and 4) perfect !