如何在Laravel 5.8.0中链接菜单页面

时间:2019-03-04 16:18:41

标签: php laravel-5 laravel-5.8

我在菜单栏中遇到了无法正确显示结果的问题。我要分享几行。

index.blade.php:-

<nav class="navbar navbar-expand-lg navbar-light">
  <a class="navbar-brand" href="">Easy Service</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav ml-auto">
      <li class="nav-item ">
        <a class="nav-link" href="">Home </a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="about">About</a>
      </li>
        <li class="nav-item">
        <a class="nav-link" href="services">Services</a>
      </li>

      <li class="nav-item">
        <a class="nav-link " href="contact">Contact</a>
      </li>

      <li class="es">
        <a href="#" class="nav-link" data-toggle="modal" data-target="#myModal">Login</a>
      </li>
    </ul>
</nav>

这是另一个文件。

web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('index');
});
Route::get('/about', function () {
    return view('about');
});
Route::get('/contact', function () {
    return view('contact');
});
Route::get('/services', function () {
    return view('services');
});

当我通过单击链接从主页打开页面时,输出是正确的,但是当我通过其他页面打开链接时,主页链接不起作用。 如果我将(/index)放在 web.php 文件中,则它可以工作,但是我不想在URL中显示(/index)的主页链接。 只能通过在斜杠(/)后显示带有URL的名称来打开它。

如何链接主页而不在浏览器上显示URL?

Home Page with Empty slash Home Page with Empty slash About Page with Non-empty slash About Page with Non-empty slash

1 个答案:

答案 0 :(得分:1)

如果应该引用使用href的索引页,请不要在<a>中使用空的<a href="/">属性,因为当未填充href时,链接将指向当前页面。或者,您可以在Laravel https://laravel.com/docs/5.8/urls#urls-for-named-routes

中使用命名路由