Skip to content

Instantly share code, notes, and snippets.

@marktopper
Last active December 24, 2020 20:55
Show Gist options
  • Save marktopper/ad3f8048a1a7bf55a5fcc747745d92ec to your computer and use it in GitHub Desktop.
Save marktopper/ad3f8048a1a7bf55a5fcc747745d92ec to your computer and use it in GitHub Desktop.
[Voyager] Routes for Pages BREAD
<?php
class PageController extends \App\Http\Controllers\Controller
{
public function show()
{
$slug = request()->segment(1);
$page = \TCG\Voyager\Models\Page::where('slug', $slug)
->firstOrFail();
return view('show-page', [
'page' => $page,
]);
}
}
<?php
try {
$pages = \TCG\Voyager\Models\Page::all();
foreach ($pages as $page) {
Route::get($page->slug, 'PageController@show');
}
} catch (\Exception $exception) {
// do nothing
}
<h1>{{ $page->title }}</h1>
<?php echo $page->body; ?>
@redsoxfan2499
Copy link

Thanks Mark. The above code did not work out of the box. I kept getting errors and noticed that in the Page Controller, the $slug was always null. So after some testing, I changed the following line

$slug = request()->segment(0);
to
$slug = request()->segment(1);

and now works:

Thanks for making the Voyager system.

@marktopper
Copy link
Author

Ahh thanks for clearing that our @redsoxfan2499 👍
I have updated the gist.

@olko404
Copy link

olko404 commented Apr 26, 2017

sorry for my stupid question, maybe you can give a cue me how to display post from voyager ( e.g. that post that I got from "php artisan voyager:install --with-dummy" command, how to get them in my view.

@Dynamo6
Copy link

Dynamo6 commented May 25, 2017

Thanks Mark - worked for me too. This stuff really needs to be added to the official docs!

@unknownFrequency
Copy link

unknownFrequency commented Jun 5, 2017

How about :

Route::get('/pages/{slug}', 'PagesController@getPage');   <-- web.php

 public function getPage($slug)
{
    $page = DB::table('pages')->where('slug', $slug)->firstOrFail();
    return view('pages/index', ['page' => $page]);
}

@jrpikong
Copy link

jrpikong commented Jul 20, 2017

Hi @marktopper how to modification Post admin page , I will install or add module tags in post add,
btw I uses : https://cartalyst.com/manual/tags/4.0#adding-tags

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment