dousu1900 2016-10-01 15:32
浏览 36
已采纳

在eloquent创建不使用PHP

I created a MVC in PHP and i tried to use Eloquent to handle db. But i'm stuck in the begining of it i.e; in Create itself.

my codes :

home.php:-

class Home extends Controller
{

 public function create($username='',$email='')
 {
   User::create([
  'username'=>$username,
  'email'=>$email
]);
   }
 }

?>

Controller.php

<?php

class Controller
{
  public function model($model)
 {
    require_once '../app/models/'. $model .'.php' ;
    return new $model();
  }
   public function view($view,$data=[])
 {
    require_once '../app/views/'. $view.'.php';
 }
  }

 ?>

User.php

 <?php

use Illuminate\Database\Eloquent\Model as Eloquent;

class User extends Eloquent
{
  public $name;
  protected $fillable=['username','email'];
}

?>

App.php

  <?php

  class App
 {
protected $controller = 'home';
protected $method = 'index';
protected $params = [];
   // by default we are considering /public/controller/methodname
     //as soon as the directory is written on the address bar the first arg is           controller , 2nd is method and the
   // others are parameters
      public function __construct()
      {
     $url = $this->parseUrl();
     if(file_exists('../app/controllers/'. $url[0].'.php'))
     {
       $this->controller= $url[0];
       unset($url[0]); //remove fromm array
     }    //this if was to check whether the entered controller exists or not, if exists controller is set to the controllers name
         //or home is the default controller
     require_once '../app/controllers/'. $this->controller.'.php';
     $this->controller= new $this->controller;// create object instance of that controller
     if(isset($url[1]))
     {
       if(method_exists($this->controller, $url[1]))
       {
         $this->method = $url[1];
         unset($url[1]);
       }
     }

     $this->params= $url ? array_values($url) : [];
     call_user_func_array([$this->controller,$this->method], $this->params);
}
public function parseUrl()
{
  if(isset($_GET['url'])){
    return $url = explode('/',filter_var(rtrim($_GET['url'],'/'),FILTER_SANITIZE_URL));
  }
       }
  }

   ?>

in my users table in Mysql only the created_at and updated_at gets updated after each time i hit the url.

enter image description here

format of url: public/controller/method/param1/param2

url: public/home/create/Johnny/johnny16@live.com

  • 写回答

1条回答 默认 最新

  • duanjiaopi8218 2016-10-01 15:55
    关注

    Please Check the data type in mysql table. It should be compatible with data you using in array of create meathod. i.e It should be varchar(length).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度