简体   繁体   English

Altorouter 表单重定向

[英]Altorouter form redirection

I am using Alto router for a project.我正在为一个项目使用 Alto 路由器。 My problem comes when I submit a form, I can't find a solution to make redirection works.当我提交表单时,我的问题出现了,我找不到使重定向工作的解决方案。 My project structure:我的项目结构:

root
|/elements
|- layout.php
|/public
|- index.php
|/templates
|- add.php
|- home.php
|- login.php

I tried diffents url in the action attributes.我在动作属性中尝试了不同的 url。 I also tried to leave it blank and use header("Location") to redirect.我还尝试将其留空并使用 header("Location") 进行重定向。

Here's how I handle the router:这是我处理路由器的方式:

$router = new AltoRouter();
$router->map('GET', '/', 'home', 'home');
$router->map('GET', '/login', 'login', 'login');
$router->map('GET', '/add', 'add', 'add');
$match = $router->match();

if (is_array($match)) {
    if (is_callable($match['target'])) {
        call_user_func_array($match['target'], $match['params']);
    } else {
        $params = $match['params'];
        ob_start();
        require "../templates/{$match['target']}.php";
        $pageContent = ob_get_clean();
    }
    require '../elements/layout.php';

} else {
    echo '404';
}

Now the on the add page I have a form that should add to my db and then redirect to home page.现在在添加页面上,我有一个应该添加到我的数据库然后重定向到主页的表单。 This is where I'm stuck (also, the db part for inserting might contain errors but i'll work on it later):这是我卡住的地方(另外,用于插入的 db 部分可能包含错误,但我稍后会处理它):

<?php

use App\App;

if (!empty($_POST)) {
    $he = App::getPDO()->prepare("INSERT INTO huiles(name_simple, name_science, elements, dilution, props) VALUES (?, ?, ?, ?, ?)");
    $params = [
        $_POST['name_simple'],
        $_POST['name_science'],
        $_POST['elements'],
        $_POST['dilution'],
        $_POST['props']
    ];
    $he->execute($params);
}

?>

<form action="<?= $router->generate("home") ?>" method="post">
<div class="form-group">
    <label for="name_simple">Nom</label>
    <input type="text" name="name_simple" class="form-control">
</div>
<div class="form-group">
    <label for="name_simple">Nom scientifique</label>
    <input type="text" name="name_science" class="form-control">
</div>
<div class="form-group">
    <label for="name_simple">Elements</label>
    <input type="text" name="elements" class="form-control">
</div>
<div class="form-group">
    <label for="name_simple">Dilution</label>
    <input type="text" name="dilution" class="form-control">
</div>
<div class="form-group">
    <label for="name_simple">Propriétés</label>
    <input type="text" name="props" class="form-control">
</div>
<button class="btn btn-primary">Ajouter</button>
</form>

How should I handle the router to redirect to home page after the submit (and also after the data is added to the db)?提交后(以及将数据添加到数据库后),我应该如何处理路由器重定向到主页?

You must first define your route to your processing file with the POST method because you are sending a form:您必须首先使用 POST 方法定义处理文件的路径,因为您正在发送表单:

$router->map('POST', '/treatement', function() {
    require __DIR__ . 'treatement.php';
});

In your view:在您看来:

<form method="post" enctype="multipart/form-data" action="/treatement">

In treatement.php在治疗中。php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
       if (// If form is not complete, error, etc ..) {
        echo '<h1>Failed to send</h1>
        <a href="/form"<button type="button">
                Come back to the form 
       </button></a>';
    } else {
    //Insert database and redirect to the home for example
         echo '<h1>Success</h1>
        <a href="/home"<button type="button">
                Come back to the home 
       </button></a>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM