我的路线:
class Route
{
static function start()
{
$controller_name = 'add_task';
$action_name = 'index';
$routes = explode('/', $_SERVER['REQUEST_URI']);
if ( !empty($routes[1]) )
{
$controller_name = $routes[1];
}
if ( !empty($routes[2]) )
{
$action_name = $routes[2];
}
$model_name = 'Model_'.$controller_name;
$controller_name = 'Controller_'.$controller_name;
$action_name = 'action_'.$action_name;
$model_file = strtolower($model_name).'.php';
$model_path = "application/models/".$model_file;
if(file_exists($model_path))
{
include "application/models/".$model_file;
}
$controller_file = strtolower($controller_name).'.php';
$controller_path = "application/controllers/".$controller_file;
if(file_exists($controller_path))
{
include "application/controllers/".$controller_file;
}
else
{
Route::ErrorPage404();
}
$controller = new $controller_name;
$action = $action_name;
if(method_exists($controller, $action))
{
$controller->$action();
}
else
{
Route::ErrorPage404();
}
}
我的.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
我的控制器:
<?php
class Controller_tasks extends Controller
{
function __construct()
{
$this->model = new Model_tasks();
$this->view = new View();
}
function action_index()
{
if (isset($_GET['page'])){
$data = $this->model->get_data_from_server($_GET['page']);
}
else{
$data = $this->model->get_data_from_server($page = 1);
}
$this->view->generate('tasks_view.php', 'template_view.php', $data);
}
}
我的观点:
<a href="tasks?page=2">← Back</a>
发送页码需要做什么?如何在mvc中实现这个? 我的路线只能获取动作名称,但不能获取变量。有人可以帮我解决这个问题吗?
答案 0 :(得分:1)
我个人使用非常相似的东西。但是,当我在斜杠上分割URL时,我进一步进行了一步,我将所有其他部分分配为变量,然后将这些部分作为参数传递给函数。
我会显示完整的代码,但您真正感兴趣的位于分割网址功能中。
<?php
namespace Core;
use Core\Face\RouterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class Router implements RouterInterface
{
/** @var null The bundle */
private $bundle = null;
/** @var null The controller */
private $controller = null;
/** @var null The method (of the above controller), often also named "action" */
private $action = null;
/** @var array URL parameters */
private $params = array();
function getBundle()
{
return $this->bundle;
}
function getController()
{
return $this->controller;
}
function getAction()
{
return $this->action;
}
function getParams()
{
return $this->params;
}
function setBundle($bundle)
{
$this->bundle = ucfirst(strtolower($bundle)); // The bundle
}
function setController($controller)
{
$this->controller = ucfirst(strtolower($controller)) . "Controller"; // The controller we want.
}
function setAction($action)
{
$this->action = ucfirst(strtolower($action)) . "Action"; // The action within the controller we want.
}
function setParams($params)
{
$this->params = $params; // These become variables passed straight into the controller.
}
/**
* "Start" the application:
* Analyze the URL elements and calls the according controller/method or the fallback
*/
public function __construct(Request $request)
{
$this->getClass($this->splitUrl($request));
}
public function getClass()
{
$namespace = "{$this->getBundle()}"\\Controller
if (class_exists($namespace)) { // Namespace exists, and method found.
$class = new $namespace;
// check for method: does such a method exist in the controller ?
if (method_exists($class, $this->getAction())) {
return call_user_func_array(array($class, $this->getAction()), $this->getParams());
} elseif (method_exists($class, "indexAction")) {
return call_user_func(array($class, "indexAction"));
} else {
throw new NotFoundHttpException("Page not found");
}
} else { // Else show index page
return (new \IndexController())->indexAction();
}
}
/**
* Get and split the URL
*/
public function splitUrl(Request $request)
{
if ($request->query->get('url')) {
// split URL
$url = explode('/', filter_var(trim($request->query->get('url'), '/'), FILTER_SANITIZE_URL));
// Put URL parts into according properties
$this->setBundle(@$url[0]);
$this->setController(@$url[1]);
$this->setAction(@$url[2]);
// Remove controller and action from the split URL
unset($url[0], $url[1], $url[2]);
// Rebase array keys and store the URL params
$this->setParams(array_values($url));
return;
}
}
}
在视图中使用该方法而不是使用:
<a href="tasks?page=2">← Back</a>
这将是:
<a href="tasks/2">← Back</a>
您可以在相应的功能中选择:
class Pages { // Or what ever your class/controller is.
public function tasks($page = 1){ // Set a default for good measure.
print $page; // Using the example this would be 2
}
}
.htaccess在这里:https://github.com/BonnieDoug/DougHayward/blob/master/.htaccess
你也可以在我的GitHub上获取我粘贴的代码:https://github.com/BonnieDoug/DougHayward/blob/master/Application/Core/Router.php