Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP MVC结构与动态URL重写_Php - Fatal编程技术网

PHP MVC结构与动态URL重写

PHP MVC结构与动态URL重写,php,Php,我遵循youtube播放列表构建了一个基于PHP的简单MVC框架,下面是我遵循的播放列表的一个示例 这是我的应用程序结构的图像 我已将应用程序连接到MySql数据库,一切正常,但现在我正在尝试创建友好的URL 目前,我的URL如下所示: mywebsite.com/home/listings/ 这很好,因为此链接用于静态页面,但我希望动态URL也使用此链接,例如: 当前URL:mywebsite.com/home/listings/apart?id=10 至URL:mywebsite.com

我遵循youtube播放列表构建了一个基于PHP的简单MVC框架,下面是我遵循的播放列表的一个示例

这是我的应用程序结构的图像

我已将应用程序连接到MySql数据库,一切正常,但现在我正在尝试创建友好的URL

目前,我的URL如下所示:

mywebsite.com/home/listings/
这很好,因为此链接用于静态页面,但我希望动态URL也使用此链接,例如:

当前URL:
mywebsite.com/home/listings/apart?id=10

至URL:
mywebsite.com/home/listings/apart/id/10

我在htaccss中尝试了很多方法,但一旦URL被重新写入,数据就不会从数据库中提取,我只得到一个空页面

当前.htaccess代码(位于公用文件夹中的代码):

我尝试了很多方法和规则,但没有成功

我不确定我提供的细节是否足够清晰,我有点困惑,因为我将许多教程结合在一起创建了这个应用程序

App.php代码:

<?php
  class App{
    protected $controller = 'home';
    protected $method = 'index';
    protected $params = [];
    public function __construct(){
      $url = $this->parseUrl();
        if(file_exists('../app/controllers/' . $url[0] . '.php')){
          $this->controller = $url[0];
          unset($url[0]);
        }
        require_once '../app/controllers/' .$this->controller. '.php';
        $this->controller = new $this->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));
        echo $url;
      }
    }
  }
 ?>

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';
    }
  }
 ?>

控制器代码:

<?php
  class home extends Controller{
    public function index(){
      $this->view('home/index');
    }
    public function listings(){
      $this->view('home/listings');
    }
    public function property(){
      $this->view('home/property');
    }
  }
 ?>

该项目处于联机状态,如果有人有兴趣帮助我解决此问题,我可以提供完全访问权限。

控制器方法需要参数。应用程序调用需要特定的参数。 在app.php的
call\u user\u func\u array()
中,您已经尝试将参数传递给正在调用的控制器方法。您只需要方法具有参数

请注意,您需要对args进行一些验证,因为任何人都可以在那里放置任何东西

我能给出的最简单的答案是:

/app/controllers/home.php 请记住以下注意事项:

  • 有很多方法可以构造这种结构,这里的逻辑可以是应用程序、基本控制器或模型中的逻辑
  • 清理$value,或者构建模型,这样就不重要了
  • 您仍然需要找到在视图中使用这些参数的方法

你能发布
core/Controller.php的代码吗
假设它有解析
$\u GET['url']
,顺便说一句,没有人想坐在youtube上看你的文章,请在这里发布。@Lawrencerone感谢你对我的文章感兴趣,我只是提供了youtube链接,以便对我所遵循的教程有一个清晰的了解,但当然,我不希望任何人用它来解决我的问题。无论如何,我已经包含了控制器代码,请告诉我是否有任何其他代码可以帮助解决问题。好的,在应用程序代码中,它返回到
$url
中,你应该将它放在
$this->params
中,就像
$this->params['params']
,然后,您将能够从控制器方法访问正在进行的URL路径。无法区分哪个,但它将位于控制器文件中的methodName($somthing)`中。发布一个控制器mmetods代码如果你需要进一步的帮助实际上再看一眼,我看到它已经被传递为
$this->params
,所以发布你的一个控制器方法的代码done,我已经包含了代码我已经包含了App.php代码,如果您需要任何其他代码来帮助解决此问题,请告诉我。@Ahmadtahan看一看。谢谢,但我对您的答案有很多疑问,因为这是我的第一个基于MVC和OOP的PHP项目,您在controller.PHP中构建模型是什么意思,您的意思是构建findAndLoadData方法吗,你所说的仅允许的字段是什么意思,你是指MySQL数据库中存在的字段。我做了以下操作,但它成功了,我不知道这是否是一种有效的方法,
公共函数属性($params){$this->view($home/property',$\u GET['id']=$params);}
@Ahmad,通过“构建模型”,我是指从数据库收集数据,并将其转换为PHP中的对象。这些模型(MVC中的M)应该是明确定义的类,也是应用程序其余部分与DB交互的唯一方式。构建这些模型的代码应该是核心的一部分(在app.php中),或者在基本Controller类中。
<?php
  class home extends Controller{
    public function index(){
      $this->view('home/index');
    }
    public function listings(){
      $this->view('home/listings');
    }
    public function property(){
      $this->view('home/property');
    }
  }
 ?>
public function listings($type = '', $field = null, $value = null){
    $viewParams = [];

    // build model (much of this should be in controller.php)
    // only allow known types
    if (in_array($type, ['apartment','house']) {
        $model = $this->model($type);

        // again, only certain fields (this should be handled in model.php)
        if (in_array($field, ['id','allowedField']) {

            // this is where things can get REALLY dicey. 
            // you'll need to sanitize the value, AND make sure the data-type is compatible with the field
            $modelData = $model->findAndLoadData([$field => $value]);
            $viewParams['type'] = $type;
            $viewParams[$type] = $modelData;
        }
    }

    $this->view('home/listings', $viewParams);
}