RobinJ1995

MVC example

Jul 30th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. # Route
  2. Route::model ('page', 'Page'); // The {page} variable is an instance of the Page model
  3. Route::get ('/p/{page}', 'PageController@show'); // The show method in the PageController controller will handle this URL with {page} as a parameter
  4.  
  5. # Model
  6. class Page extends Eloquent
  7. {
  8.     protected $table = 'page'; // Use the page table in the database
  9.  
  10.     public function author ()
  11.     {
  12.         return $this->belongsTo ('Author'); // This Page belongs to an Author
  13.     }
  14. }
  15.  
  16. class Author extends Eloquent
  17. {
  18.     protected $table = 'author'; // Use the author table in the database
  19. }
  20.  
  21. # Controller
  22. class PageController extends BaseController
  23. {
  24.     public function show ($page)
  25.     {
  26.         return View::make ('page', compact ('page')); // Make the view page.blade.php and pass the $page variable (which is an instance of the Page model) to it
  27.     }
  28. }
  29.  
  30. # View (filename: page.blade.php)
  31. <!DOCTYPE html>
  32. <html>
  33. <head>
  34.     <title>{{ $page->title }}</title>
  35. </head>
  36. <body>
  37.     <h1>{{ $page->title }}</title>
  38.     <p id="author">Author: {{ $page->author->name }}</p>
  39.     <div id="content">
  40.         {{ $page->content }}
  41.     </div>
  42. </body>
  43. </html>
Advertisement
Add Comment
Please, Sign In to add comment