Romanovich095

MVC using

Aug 4th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Define a namespace You will be using
  4.  */
  5. namespace App\Test;
  6.  
  7. /**
  8.  * Include all classes You used to use
  9.  */
  10. use Table\UsersTable;
  11. use Controller;
  12.  
  13. /**
  14.  * Controllers\PagesController.php
  15.  *
  16.  * Class PagesController
  17.  * @package App\Test
  18.  * @property UsersTable $Users
  19.  */
  20. class PagesController extends Controller
  21. {
  22.  
  23.     /**
  24.      * Initializing Models and additional configurations
  25.      */
  26.     public function initialize()
  27.     {
  28.         $this->loadModel('Users');
  29.     }
  30.  
  31.     /**
  32.      * Fetching users list and passing it to View
  33.      */
  34.     public function index()
  35.     {
  36.         $this->set('users', $this->Users->getAllUsers());
  37.         // OR
  38.         $users = $this->Users->getAllUsers();
  39.         $this->set(compact('users'));
  40.     }
  41. }
  42.  
  43.  
  44. /**
  45.  * Models\UsersTable.php
  46.  *
  47.  * Class UsersTable
  48.  * @package App\Test
  49.  */
  50. class UsersTable extends Table
  51. {
  52.     /**
  53.      * Returns all users list
  54.      * @param $order string asc|desc
  55.      * @param $field string
  56.      * @return array
  57.      */
  58.     public function getAllUsers($order = 'asc', $field = 'id')
  59.     {
  60.         return $this->find('all')
  61.             ->order([$field => $order])
  62.             ->toArray();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment