简体   繁体   中英

how to make working menu tab with laravel 5.4

I am working on Laravel 5.4 I have created a menu, in which three tabs Home, about and contact. When I click on Home then it should be on home page. When click on about, it should be on about page....

web.php:

<?php

Route::get('/', function()
{
return View('method1.home');
});
Route::get('about', function()
{
return View('method1.about');
});

**method1 is folder in resources\\views**

home.blade.php:

@extends('method1.dashboard')
@section('content')
 <h1>This is home page</h1>
@endsection

about.blade.php is:

@extends('method1.dashboard')
@section('content')
 <h1>This is about page</h1>
@endsection

dashboard.blade.php is:

@include('method1.includes.menu-header')

menu-header.blade.php is:

<li class="active"> <a href="/">Home</a></li>
<li> <a href="/about">About</a></li>

But when I click on home or about page. It shows Page is not found.

My laravel projet folder name is admin_laravel . When I run http://localhost/admin_laravel/ then it shows home page and when run http://localhost/admin_laravel/about it shows about page.

But when I click on about menu button then in browser shows link http://localhost/about . Means it is not going with http://localhost/admin_laravel/about and page is not showing.

You are missing something there, For a quick fix you can do this:

<li class="active"> <a href="{{ url('/') }}">Home</a></li>
<li> <a href="{{ url('about') }}">About</a></li>

You can also give route name and go with route method:

Route::get('about', function()
{
   return View('method1.about');
})->name('about');

Then:

<li> <a href="{{ route('about') }}">About</a></li>

Here is the details: https://laravel.com/docs/5.2/helpers#method-route

You're hard-coding your URLs. When you have <li><a href="/about">About</a></li> you're telling your browser to go to the about path from the root of the domain (which is what happens when you prefix your URL with / ), which in this case is http://localhost/ .

There's a few things you should be doing. First, set the base URL for your project, you can update APP_URL in your .env file

APP_URL=http://localhost/admin_laravel

or the url option in config/app.php .

'url' => env('APP_URL', 'http://localhost/admin_laravel'),

Secondly, when generating URLs in Laravel there are quite a few options. If you're not using named routes, then you should use the url helper method to generate your URLs:

<li><a href="{{ url('about') }}">About</a></li>

That will make sure that your URL is based at the root of your project, rather than the root of the domain. When you use url in conjunction with the correct setting as described above, your URLs will be generated correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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