0

Changed the source code so it now works properly.

I created a dynamic navigation for the frontend page. However, I had a little problem with it. When I navigate to the page of one of the entries in the blog menu item (for example: 127.0.0.1:8000/blog/one-post), I move the mouse over any of my menu items in the navigation bar, and the paths of all my menu items receive the /blog prefix.

The content parts are attached to the pages, so I can add more content elements to each page on the administration page.

So, normally I have these routes:

If I visit a blog post (localhost/laravelprojekt/blog/one-post), the url link of each menu item will be this if I move the mouse over the menu items:

My codes are:

routes\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;
    }
}

resources\views\components\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

Specifying the / sign within the parameter or in the page database slug field.

Thanks for the help brombeer

1
  • Set up vhosts for your webserver or use php artisan serve to serve your projects. How do you assemble the links in your nav? Commented Feb 25, 2024 at 16:41

1 Answer 1

-1

The server is of course running (php artisan serve).

The menu is structured as follows:

resources\views\navigation-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
Sign up to request clarification or add additional context in comments.

6 Comments

This is not really an answer to your question. Please edit your question and post relevant information there. Thanks
"The server is of course running (php artisan serve)." That is strange, since your urls are localhost/laravelprojekt. If you had php artisan serve running, from your project folder (assuming laravelprojekt is your project folder) your urls should be http://localhost:8000/home, http://localhost:8000/blog etc
I only wrote the route for illustration. The point is that if I visit a blog post and then move the mouse to another menu item, the url will be /blog/history or /blog/contact and I will get a 404 error page.
Start URLs in a link with / to start from the topmost folder of your server's "DocumentRoot". <a href="/{{ $item->slug }}".... When you are inside the /blog folder, linking to href="home" will link to /blog/home, but linking to href="/home" would take you to /home. (For this to work your project should be served from the project folder, not some subfolder of your localhost)
Yes, I just noticed after typing the comment that the url doesn't include the / sign. Thanks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.