来自数据库的 Laravel 动态导航栏

问题描述 投票:0回答:1

我为前端页面创建了动态导航。但是,我遇到了一点问题。当我导航到博客菜单项中的某个条目的页面(例如:localhost/laravelprojekt/blog/one-post)时,我将鼠标移动到导航栏中的任何菜单项上,并且路径我的所有菜单项都带有 /blog 前缀。

内容部分附加到页面上,因此我可以向管理页面上的每个页面添加更多内容元素。

所以,通常我有这些路线:

  • localhost/laravelprojekt/home
  • 本地主机/laravelproject/博客
  • 本地主机/laravelproject/关于
  • localhost/laravelproject/历史
  • localhost/laravelprojekt/联系人

如果我访问一篇博客文章(localhost/laravelprojekt/blog/one-post),如果我将鼠标移到菜单项上,每个菜单项的 url 链接将是这样的:

  • localhost/laravelprojekt/blog/home
  • localhost/laravelproject/博客/博客
  • localhost/laravelprojekt/博客/关于
  • localhost/laravelprojekt/博客/历史
  • localhost/laravelprojekt/博客/联系人

我的代码是:

路线\web.php

Route::group(['prefix' => 'blog'], function () {
    Route::get('/', [PostController::class, 'index'])->name('posts.index');
    Route::get('/{slug}', [PostController::class, 'show'])->name('posts.show');
});

Route::get('/', HomeController::class)->name('home');
Route::get('/contact', [HomeController::class, 'contact'])->name('pages.contact');
Route::get('/{slug}', [HomeController::class, 'show'])->name('pages.show');

App\Http\Controllers\HomeController.php

class HomeController extends Controller
{

    public function __invoke(Request $request)
    {        
        $featuredPosts = Post::published()->featured()->with('categories')->latest('published_at')->take(4)->get();

        $latestPosts = Post::published()->with('categories')->latest('published_at')->take(12)->get();

        $page = self::pageData('home');

        return view('home', [
            'featuredPosts' => $featuredPosts,
            'latestPosts' => $latestPosts,
            'page' => $page,
        ]);
    }

    public function contact()
    {
        $page = self::pageData('contact');

        return view('pages.show-contact', [
            'page' => $page,
        ]);
    }

    public static function show($slug)
    {
        $page = self::pageData($slug);

        if($page->slug == "home")
        {
            return redirect()->route('home');
        }

        return view('pages.show', [
            'page' => $page,
        ]);
    }

    public static function pageData($slug)
    {
        abort_unless($page = Page::whereSlug($slug)->first(), 404);

        return $page;
    }
}

App\Http\Controllers\PostController.php

class PostController extends Controller
{
    public function index()
    {
        $categories = Category::whereHas('posts', function ($query) {
           $query->published();
        })->take(10)->get();

        $page = self::pageData('blog');

        return view(
            'posts.index',
            [
                'categories' => $categories,
                'page' => $page,
            ]
        );
    }

    public function show($slug)
    {
        abort_unless($post = Post::whereSlug($slug)->first(), 404);

        return view(
            'posts.show',
            [
                'post' => $post
            ]
        );
    }

    public static function pageData($slug)
    {
        abort_unless($page = Page::whereSlug($slug)->first(), 404);

        return $page;
    }
}
laravel laravel-blade laravel-routing laravel-controller
1个回答
0
投票

服务器当然正在运行(php artisanserve)。

菜单结构如下:

资源视图 avigation-menu.blade.php

@foreach ($menu as $item)
    <li>
        <a href="{{ $item->slug }}" class="inline-flex items-center hover:text-yellow-900 text-sm text-gray-500">
            {{ $item->name }}
        </a>
    </li>
@endforeach
© www.soinside.com 2019 - 2024. All rights reserved.