-2
\$\begingroup\$

Aim: To make pages located in site.com/page-1, site.com/page-2. Those pages are pretty much static so I won't use database.

First, I've created the PageController.

class PageController extends Controller
{

 public function page($slug)
 {
  $page = $this->pageContents($slug);
  return view('page', ['page' => $page]);
 }

 public function pageContents($slug)
 {
  $page = [];
  $page["slug"] = $slug;
  if ($slug == "page-1") {
   $page["title"] = "Page 1";
   $page["content"] = "Content..";
  }

  if ($slug == "page-2") {
   $page["title"] = "Page 2";
   $page["content"] = "Content..";
  }  

  return $page;
 }
}

web.php (Routes):

Route::get('/{page-1}', 'PageController@page')->name('page-1');
Route::get('/{page-2}', 'PageController@page')->name('page-2');

Would appreciate any reviews!

\$\endgroup\$
2
\$\begingroup\$

Not sure that it makes much difference, but I find this simpler

class PageController extends Controller
{

 public function page1()
 {
   return view('page', [
    'page' => [
        'slug' => 'page-1',
        'title' => 'Page 1',
        'content' => 'Content..',
    ]
   ]);
 }

 public function page2()
 {
   return view('page', [
    'page' => [
        'slug' => 'page-2',
        'title' => 'Page 2',
        'content' => 'Content..',
    ]
   ]);
 }

}

web.php (Routes):

Route::get('/page-1', 'PageController@page1')->name('page-1');
Route::get('/page-2', 'PageController@page2')->name('page-2');
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.