php - PHP MVC路由> 如何创建自定义路由?

  显示原文与译文双语对照的内容
0 0

我在我的网站上使用了 PHP MVC,我对路由有一个问题。

转到索引( 首页) 时,使用 http://www.example.com 或者 http://www.example.com/index 。

当我进入联系人页面时,我使用 http://www.example.com/contact

当我访问服务或者关于页面时,我使用 http://www.example.com/content/page/services 或者 http://www.example.com/content/page/about 。

我的索引和联系页有自己的控制器,因为它们是 static 页面。 但是,服务和关于页面是从我的数据库中提取的,因此动态。 所以我创建了一个控制器,命名为内容,只传递需要获取任何页面的参数。

我想使我的网址更加一致。 如果我转到服务或者关于页面,我想使用 http://www.example.com/services 或者 http://www.example.com/about 。

如何更改路由以满足这里要求? 我最终希望能够在数据库中创建页面,然后使用看起来像它自己控制器的URL来创建页面。 而不必调用内容控制器来使它的正常工作。

下面是我的控制器以及它们包含的方法,以及我的路由代码。

控制器:


IndexController
 function: index

ContentController
 function: page
 function: sitemap

ContactController
 function: index
 function: process

路由


class Application
{
//@var mixed Instance of the controller
private $controller;

//@var array URL parameters, will be passed to used controller-method
private $parameters = array();

//@var string Just the name of the controller, useful for checks inside the view ("where am I?")
private $controller_name;

//@var string Just the name of the controller's method, useful for checks inside the view ("where am I?")
private $action_name;

//Start the application, analyze URL elements, call according controller/method or relocate to fallback location
public function __construct()
{
//Create array with URL parts in $url
 $this->splitUrl();

//Check for controller: no controller given? then make controller = default controller (from config)
 if (!$this->controller_name) {
 $this->controller_name = Config::get('DEFAULT_CONTROLLER');
 }

//Check for action: no action given? then make action = default action (from config)
 if (!$this->action_name OR (strlen($this->action_name) == 0)) {
 $this->action_name = Config::get('DEFAULT_ACTION');
 }

//Rename controller name to real controller class/file name ("index" to"IndexController")
 $this->controller_name = ucwords($this->controller_name). 'Controller';

//Check if controller exists
 if (file_exists(Config::get('PATH_CONTROLLER'). $this->controller_name. '.php')) {

//Load file and create controller
//example: if controller would be"car", then this line would translate into: $this->car = new car();
 require Config::get('PATH_CONTROLLER'). $this->controller_name. '.php';
 $this->controller = new $this->controller_name();

//Check for method: does such a method exist in the controller?
 if (method_exists($this->controller, $this->action_name)) {
 if (!empty($this->parameters)) {
//Call the method and pass arguments to it
 call_user_func_array(array($this->controller, $this->action_name), $this->parameters);
 } else {
//If no parameters are given, just call the method without parameters, like $this->index->index();
 $this->controller->{$this->action_name}();
 }
 } else {
 header('location: '. Config::get('URL'). 'error');
 }
 } else {
 header('location: '. Config::get('URL'). 'error');
 }
}

//Split URL
private function splitUrl()
{
 if (Request::get('url')) {

//Split URL
 $url = trim(Request::get('url'), '/');
 $url = filter_var($url, FILTER_SANITIZE_URL);
 $url = explode('/', $url);

//Put URL parts into according properties
 $this->controller_name = isset($url[0])? $url[0] : null;
 $this->action_name = isset($url[1])? $url[1] : null;

//Remove controller name and action name from the split URL
 unset($url[0], $url[1]);

//rebase array keys and store the URL parameters
 $this->parameters = array_values($url);
 }
 }
}

时间: 原作者:

0 0

要做到这一点,你应该将你的网址映射到控制器,检查以下示例:


//route mapping 'route' => 'controller:method'
$routes = array(
 '/service' => 'Content:service'
);

控制器也可以是任何php可以调用函数。

答案版本 2:

在最简单的模式下,让我们假设你有如下所示的实体:


uri: varchar(255), title: varchar(255), meta_tags: varchar(500), body: text

并且可以从 www.example.com/page/ url访问 StaticPageController,以及这里url将以 uri 参数传递到控制器之后的内容。


public function StaticPageController($uri){
//this can return a page entity
//that contains what ever a page needs.
 $page = $pageRepository->findByUri($uri)

//pass it to view layer
 $this->renderView('static_page.phtml', array('page' => $page));
}

我希望这可以帮到你。

...