将参数传递给路由PHP

时间:2017-04-06 12:16:16

标签: php .htaccess

我设置了一个基本的路由系统,并希望通过url传递参数以用作ID

按钮视图是:

<p><a href="/card/?idcard=<?= $carded->getId(); ?>" class="btn btn-primary" role="button">View More</a></p>

给我一​​个网址:card /?idcard = xy11-25

卡片页面的控制器是:  

use Pokemon\Pokemon;

$id = Pokemon::Card()->find($_GET['idcard']);

require 'views/card.view.php';

,路径文件看起来像

$router->define([
    '' => 'controllers/index.php',
    'about' => 'controllers/about.php',
    "card" => 'controllers/card.php',
    'contact' => 'controllers/contact.php',
    'search' => 'controllers/search.php'
]);

最后我的.htaccess文件是:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

希望这是有道理的

编辑|添加路由器类

class Router {
    protected $routes = [];

    public static function load($file) {
        $router = new static;
        require $file;
        return $router;
    }

    public function define($routes){
        $this->routes = $routes;
    }

    public function direct($uri){
        if (array_key_exists($uri, $this->routes)){
            return $this->routes[$uri];
        }

        throw new Exception('No route defined for this URI.');
    }
}

所以,目前路由器正在抛出异常,最好的方法是从URL获取ID。

0 个答案:

没有答案