Laravel视图内容显示两次带参数的路径

时间:2017-11-10 11:15:01

标签: php laravel-5.5

我正在使用Laravel 5.5,我有两个带有这样的参数的路径

Route::get( 'route/{parameter}', 'Controller@action' )->name( 'controller.action' );

我的控制器操作只是加载视图:

public function action( $slug ) {
    return view( 'view', [
        'post' => Post::whereSlug( $slug )->first()
    ] );
}

我的基础app.blade.php查看内容是:

<!-- ==========================
  PAGE WRAPPER
=========================== -->
<div class="page-wrapper">

    <!-- ==========================
          HEADER
        =========================== -->
    <header class="header" id="header">

        <!-- Hamburger -->
        <div class="h-hamburger" id="hamburger">
            <svg class="h-hamburger__icon">
                <use xlink:href="#icon-hamburger"></use>
            </svg>
        </div>

        <!-- Logo -->
        <div class="h-logo">
            <a href="{{route('dashboard')}}" title="{{config('app.name','Sensei U')}}">
                <img class="h-logo__img" src="{{URL::to('/')}}/images/logo-black.svg"
                     alt="{{ config('app.name', 'Sensei U') }}">
            </a>
        </div>

    @if (Auth::user())

        <!-- Welcome -->
            <div class="h-welcome">

                <!-- Title -->
                <div class="h-welcome__title">Welcome Back, {{Auth::user()->name}}</div>

                <!-- Date -->
                <div class="h-welcome__date">{{date('F j')}}</div>

                <!-- Time -->
                <div class="h-welcome__time">{{date('g:i a')}}</div>

            </div>
            <!-- Profile -->
            <div class="h-profile">

                <!-- Avatar -->
                <img class="h-profile__avatar"
                     src="{{Auth::user()->image ? SenseiU::getImagePathBySize(Storage::url(Auth::user()->image), 58, 58) : ''}}"
                     srcset="{{Auth::user()->image ? SenseiU::getImagePathBySize(Storage::url(Auth::user()->image), 116, 116) : ''}} 2x"
                     alt="{{Auth::user()->name}}">
            @if(($notifications = SenseiU::generateNotificationMessages( Auth::user() )) )
                <!-- Notifications -->
                    <button class="h-profile__notifications" data-uid="{{Auth::user()->id}}"
                            data-url="{{route('member.nr', [Auth::user()])}}">
                        {{count($notifications)}}
                    </button>
                @endif

            </div>

        @if($notifications)

            <!-- Notifications Dropdown -->
                <ul class="noti-dropdown">
                    @foreach($notifications as $notification)
                        <li class="noti-dropdown__item">
                            <a class="noti-dropdown__link" href="{{$notification['link']}}">
                                {{$notification['msg']}}
                            </a>
                        </li>
                    @endforeach
                </ul>
            @endif
        @endif

    </header>
    <!-- ==========================
      CONTENT WRAPPER
    =========================== -->
    <div class="wrapper">
        <!-- ==========================
          SIDEBAR
        =========================== -->
        <aside class="sidebar" id="sidebar">
            <!-- Menu -->
            <nav class="sidebar-menu">
                @if(Auth::user())
                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'dashboard' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('dashboard')}}">
                        <span class="sidebar-menu__title">Home</span>
                    </a>

                    @permission('daily-mastery')
                    <a class="sidebar-menu__item  {{Route::currentRouteName() == 'member.daily-mastery' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.daily-mastery')}}">
                        <span class="sidebar-menu__title">Daily Mastery</span>

                        @if(SenseiU::getPageNotificationsCount( Auth::user(), [
                        'App\Notifications\TipPublishedNotification',
                        'App\Notifications\ExerciseSchedulePublishedNotification'] ))

                            <span class="sidebar-menu__notifications">{{SenseiU::getPageNotificationsCount( Auth::user(), [
                        'App\Notifications\TipPublishedNotification',
                        'App\Notifications\ExerciseSchedulePublishedNotification'] )}}</span>
                        @endif
                    </a>
                    @endpermission

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.macros' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.macros')}}">
                        <span class="sidebar-menu__title">Macros Calculator</span>
                    </a>

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.instructional-videos' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.instructional-videos')}}">
                        <span class="sidebar-menu__title">Instructional Videos</span>
                        @if(SenseiU::getPageNotificationsCount( Auth::user(), [
                        'App\Notifications\VideoPublishedNotification'] ))
                            <span class="sidebar-menu__notifications">{{SenseiU::getPageNotificationsCount( Auth::user(), [
                        'App\Notifications\VideoPublishedNotification'] )}}</span>
                        @endif
                    </a>

                    @permission('health-articles')
                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.nutrition' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.nutrition')}}">
                        <span class="sidebar-menu__title">Nutrition</span>
                        @if(SenseiU::getArticleSectionNotificationsCount( Auth::user(), 2 ))
                            <span class="sidebar-menu__notifications">{{SenseiU::getArticleSectionNotificationsCount( Auth::user(), 2 )}}</span>
                        @endif
                    </a>

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.sports-injuries' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.sports-injuries')}}">
                        <span class="sidebar-menu__title">Sports Injuries</span>
                        @if(SenseiU::getArticleSectionNotificationsCount( Auth::user(), 3 ))
                            <span class="sidebar-menu__notifications">{{SenseiU::getArticleSectionNotificationsCount( Auth::user(), 3 )}}</span>
                        @endif
                    </a>

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.mobility-stretching' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.mobility-stretching')}}">
                        <span class="sidebar-menu__title">Mobility & Stretching</span>
                        @if(SenseiU::getArticleSectionNotificationsCount( Auth::user(), 4 ))
                            <span class="sidebar-menu__notifications">{{SenseiU::getArticleSectionNotificationsCount( Auth::user(), 4 )}}</span>
                        @endif
                    </a>

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.functional-training' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.functional-training')}}">
                        <span class="sidebar-menu__title">Functional Training</span>
                        @if(SenseiU::getArticleSectionNotificationsCount( Auth::user(), 5 ))
                            <span class="sidebar-menu__notifications">{{SenseiU::getArticleSectionNotificationsCount( Auth::user(), 5 )}}</span>
                        @endif
                    </a>

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.articles' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.articles')}}">
                        <span class="sidebar-menu__title">Articles</span>
                        @if(SenseiU::getArticleSectionNotificationsCount( Auth::user(), 1 ))
                            <span class="sidebar-menu__notifications">{{SenseiU::getArticleSectionNotificationsCount( Auth::user(), 1 )}}</span>
                        @endif
                    </a>
                    @endpermission

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.faqs' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.faqs')}}">
                        <span class="sidebar-menu__title">FAQs</span>
                    </a>

                    <a class="sidebar-menu__item" href="#">
                        <span class="sidebar-menu__title">Ultimate Personal Trainer</span>
                    </a>
                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'member.profile.edit' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('member.profile.edit')}}">
                        <span class="sidebar-menu__title">Edit Profile</span>
                    </a>
                    <a class="sidebar-menu__item" href="{{URL::to('logout')}}">
                        <span class="sidebar-menu__title">Logout</span>
                    </a>
                @else
                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'login' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('login')}}">
                        <span class="sidebar-menu__title">Login</span>
                    </a>

                    <a class="sidebar-menu__item {{Route::currentRouteName() == 'register' ? 'sidebar-menu__item--active' : ''}}"
                       href="{{route('register')}}">
                        <span class="sidebar-menu__title">Register</span>
                    </a>
                @endif
            </nav>

        </aside>


        <!-- ==========================
              CONTENT
            =========================== -->
        <main class="content @yield('main-class')">
            <!-- Container -->
            <div class="@yield('container-class')">
                @if(session('msg'))
                    <div class="msg msg-{{session('status')}}">
                        {{session('msg')}}
                    </div>
                @endif
                @yield('content')
            </div>
        </main>
    </div>
</div>

我的观点内容是这样的:

@extends('layouts.app')

@section('title', $title)
@section('container-class', 'container')

@section('content')
    @if($post)
        <div class="cover" style="background-image:url('{{Storage::url($post->file)}}');"></div>
        <article class="article__container">
            <h1>{{$post->title}}</h1>
            <div>{!!$post->text!!}</div>
        </article>
    @endif
@stop

问题是我的观看内容显示重复,在实际观看内容之上有一个404,如下所示:

enter image description here

2 个答案:

答案 0 :(得分:0)

这可能是由于你帖子的slu ..如果你在slug中包含特殊字符,Laravel将无法自动解决此问题,因为它假定它是另一个URL。我假设你正在尝试查看一个像<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div id="first"> <button><h3>Random <div class ="icon" /></h3></button> </div> <div id="second"> <button><h3>Random <div class ="icon" /></h3></button> </div> <div id="third"> <button><h3>Random <div class ="icon" /></h3></button> </div> <div id="fourth"> <button><h3>Random <div class ="icon" /></h3></button> </div> <div id="last"> <button><h3>Random <div class ="icon" /></h3></button> </div> </div>这样的帖子的帖子,如果在路线中包含where语句,这将有效。你必须做这样的事情:

my/first/post

这样路由将包含一个正则表达式,它验证完整的字符串是否有效。

我不知道为什么它会显示帖子和404页面,看起来你只是在Route::get('route/{parameter}', 'Controller@action') ->name('controller.action') ->where('parameter', '([A-Za-z0-9\-\/]+)');

中包含默认的404页面

答案 1 :(得分:0)

我想出来了,我会留下一个答案供将来参考,问题不在于Laravel,这是一个javascript问题,我有这个代码在头上:

    <script>
    var ajax = new XMLHttpRequest();

    ajax.open('GET', '/images/sprites.svg', true);

    ajax.send();

    ajax.onload = function (e) {

        var div = document.createElement('div');

        div.innerHTML = ajax.responseText;

        document.body.insertBefore(div, document.body.childNodes[0]);

    };
</script>

加载一个svg sprite并将其插入要使用的页面中,ajax url在带有参数的路径上返回404,所以在页面上方显示了404页面,我使用像这样的绝对URL修复它:

ajax.open('GET', '{{URL::to('/')}}/images/sprites.svg', true);

谢谢大家的帮助。