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:
- http://127.0.0.1:8000/home
- http://127.0.0.1:8000/blog
- http://127.0.0.1:8000/about
- http://127.0.0.1:8000/history
- http://127.0.0.1:8000/contact
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:
- http://127.0.0.1:8000/blog/home
- http://127.0.0.1:8000/blog/blog
- http://127.0.0.1:8000/blog/about
- http://127.0.0.1:8000/blog/history
- http://127.0.0.1:8000/blog/contact
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
php artisan serveto serve your projects. How do you assemble the links in your nav?