コード例 #1
0
 /**
  * Example of index action for someone route with the use of the a User model
  * Using the render view
  * 
  * @access public
  * @param MVC $mvc
  * @return string
  * @throws \LogicException
  */
 public function index(MVC $mvc)
 {
     if (!$mvc->hasCvpp('pdo')) {
         throw new \LogicException('PDO don\'t exists. Register the MVC\\DataBase\\PdoProvider for use this function.');
     }
     $userModel = new User($mvc->getCvpp('pdo'));
     $users = $userModel->findAll();
     return $mvc->view()->render('User/index.html', array('users' => $users));
 }
コード例 #2
0
 /**
  * Call a action of controller
  * 
  * @access public
  * @param MVC $mvc         MVC Application object
  * @param string $method   Method or Function of the Class Controller
  * @param string $fileView String of the view file
  * @return array           Response array
  * @throws \LogicException
  */
 public final function call(MVC $mvc, $method, $fileView = null)
 {
     if (!method_exists($this, $method)) {
         throw new \LogicException(sprintf('Method "s" don\'t exists.', $method));
     }
     # Replace the view object
     $this->view = $mvc->view();
     # Arguments of method
     $arguments = array();
     # Create a reflection method
     $reflectionMethod = new \ReflectionMethod(get_class($this), $method);
     $reflectionParams = $reflectionMethod->getParameters();
     foreach ($reflectionParams as $param) {
         if ($paramClass = $param->getClass()) {
             $className = $paramClass->name;
             if ($className === 'MVC\\MVC' || $className === '\\MVC\\MVC') {
                 $arguments[] = $mvc;
             } elseif ($className === 'MVC\\Server\\HttpRequest' || $className === '\\MVC\\Server\\HttpRequest') {
                 $arguments[] = $mvc->request();
             }
         } else {
             foreach ($mvc->request()->params as $keyReqParam => $valueReqParam) {
                 if ($param->name === $keyReqParam) {
                     $arguments[] = $valueReqParam;
                     break;
                 }
             }
         }
     }
     $response = call_user_func_array($reflectionMethod->getClosure($this), $arguments);
     if (empty($response)) {
         throw new \LogicException('Response null returned.');
     }
     if (is_string($response)) {
         $this->response['body'] = $response;
     } elseif ($mvc->request()->isAjax()) {
         $this->response['body'] = $this->renderJson($response);
     } elseif (is_array($response)) {
         if (!$fileView) {
             throw new \LogicException('File view is null.');
         }
         $class = explode("\\", get_called_class());
         $classname = end($class);
         // Class without Controller
         $classname = str_replace('Controller', '', $classname);
         $file = $classname . "/{$fileView}";
         $this->response['body'] = $this->renderHtml($file, $response);
     }
     return $this->response;
 }
コード例 #3
0
 function index(MVC $mvc)
 {
     $learnUri = $mvc->urlFor('app_learn', 'route');
     return $mvc->view()->render('App/index.php', array('learnUri' => $learnUri));
 }