单击 laravel 中的锚标记时页面未路由到新页面

Page not routing to new page when clicked on anchor tag in laravel

我正在使用 laravel php framework 编写登录表单。这是代码:
Login code

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Laravel PHP Framework</title>
    <style>
        @import url(//fonts.googleapis.com/css?family=Lato:700);

        body {
            margin:0;
            font-family:'Lato', sans-serif;
            text-align:center;
            color: #999;
        }

        .welcome {
            width: 300px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -150px;
            margin-top: -100px;
        }

        a, a:visited {
            text-decoration:none;
        }

        h1 {
            font-size: 32px;
            margin: 16px 0 0 0;
        }
    </style>
</head>
<body>
    <div class="welcome">
        <form action="POST">
            User Name: <input type="text"><br>
            Password: <input type="password"><br>
            <input type="submit" value="Submit"><br>
            <a href="register.php">Click here to register</a>   
        </form>
    </div>
</body>
</html>

Registration code

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Laravel PHP Framework</title>
    <style>
        @import url(//fonts.googleapis.com/css?family=Lato:700);

        body {
            margin:0;
            font-family:'Lato', sans-serif;
            text-align:center;
            color: #999;
        }

        .welcome {
            width: 300px;
            height: 200px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin-left: -150px;
            margin-top: -100px;
        }

        a, a:visited {
            text-decoration:none;
        }

        h1 {
            font-size: 32px;
            margin: 16px 0 0 0;
        }
    </style>
</head>
<body>
    <div class="welcome">
        <form action="POST">
            First Name: <input type="text"><br>
            Last Name: <input type="text"><br>
            Password: <input type="password"><br>
            Email id: <input type="email"><br>
            <input type="submit" value="Submit"><br>
        </form>
    </div>
</body>
</html>

Routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('/register', function()
{
    return View::make('register');
});

当我点击 log in page 中的锚点 link 时,它没有被重定向到注册页面。但是当我将 localhost:8000 替换为 localhost:8000/register 时,注册页面打开。
我该如何解决?

目前,你的 links 指向 /register.php 你想要的是 /register。只需更改:

<a href="/register">Click here to register</a>

或者更好的方法,使用 Laravels 辅助函数生成 URL 甚至完整的 link:

{{ link_to('register', 'Click here to register') }}

或者如果您没有使用 Blade:

<?php echo link_to('register', 'Click here to register'); ?>

列出了大多数 URL 辅助函数 here
但是那里缺少一个重要的东西:Url::to('your/url') 将只生成一个绝对的 URL,而不需要命名你的路由或针对特定的控制器操作。

如果你像下面这样使用 blade change anchor 的 href,你需要使用 URL::to() 或 route('routeName', $params) 函数

<div class="welcome">
    <form action="POST">
        User Name: <input type="text"><br>
        Password: <input type="password"><br>
        <input type="submit" value="Submit"><br>
        <a href="{{ URL::to('register') }}">Click here to register</a>   
    </form>
</div>

阅读routing documentation

查看login.php中的代码:你的link指向"register.php",但根据你定义的路由应该是“/register”。