robsonvleite / router

A classic CoffeeCode Router is easy, fast and extremely uncomplicated. Create and manage your routes in minutes! (Crie, gerencie, agrupe e execute manipuladores ou controladores com transporte de dados via rotas da aplicação)
https://www.upinside.com.br
MIT License
175 stars 41 forks source link

Erro de redirecionamento com dados. #18

Closed RuanScherer closed 3 years ago

RuanScherer commented 4 years ago

Ao redirecionar usando o método redirect os dados não são passados para o método do controlador da rota (o array de dados chega vazio). Segue trecho de código abaixo demonstrando:

public function register(array $data): void {
    echo $this->view->render("web/auth/register", $data);
}

public function handleRegister(): void {
    if (!$_POST["name"] || !$_POST["email"] || !$_POST["password"]) {
        $this->redirectWithError("/register", "Informe todos os dados.");
    } else {
        $data = (object) filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
        $users = (new User())->find("email = :email", "email={$data->email}")->count();
        if ($users) {
            $this->redirectWithError("/register", "Já existe um usuário cadastrado com o e-mail informado.");
        } else {
            $user = new User();
            $user->name = $data->name;
            $user->email = $data->email;
            $user->password = password_hash($data->password, PASSWORD_DEFAULT);
            $user->save();

            if ($user->fail()) {
                $this->redirectWithError("/register", "Erro ao cadastrar-se, tente novamente.");
            } else {
                $this->router->redirect("/app/home");
            }
        }
    }
}

public function redirectWithError(string $route, string $error) {
    var_dump($error);
    $this->router->redirect($route, ["error" => $error]);
}
omnispecies commented 3 years ago

Olá @RuanScherer , conseguiu resolver? Estou com um problema parecido...

RuanScherer commented 3 years ago

Pior que não

walissonhms commented 3 years ago

@RuanScherer problema foi resolvido?

omnispecies commented 3 years ago

Meu problema era similar, e descobri que a solução estava no .htaccess

brunobmorais commented 3 years ago

Estou com problema de ERR_TOO_MANY_REDIRECTS. Alguém sabe como resolver?

walissonhms commented 3 years ago

@brunomoraisti como você está colocando o redirecionamento?

brunobmorais commented 3 years ago

Arquivo .htaccess RewriteEngine On Options All -Indexes

ROUTER WWW Redirect.

RewriteCond %{HTTP_HOST} !^www. [NC]

RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

ROUTER HTTPS Redirect

RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteCond %{HTTPS} off

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

ROUTER URL Rewrite

RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)$ index.php?route=/$1 [L,QSA]

walissonhms commented 3 years ago

Arquivo .htaccess RewriteEngine On Options All -Indexes

ROUTER WWW Redirect.

RewriteCond %{HTTP_HOST} !^www. [NC]

RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

ROUTER HTTPS Redirect

RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteCond %{HTTPS} off

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

ROUTER URL Rewrite

RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)$ index.php?route=/$1 [L,QSA]

Seu .htaccess está correto. Já tentou acessar por outro navegador?

omnispecies commented 3 years ago

O meu tem funcionado assim

RewriteCond %{REQUEST_URI} !^/index\.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php [L]