[英]How to Read/Render URLs Like Zend Framework
我正在创建自己的PHP框架。
除了根据URL使用框架之外,一切都按计划进行。
我无法理解这:
从www.mydomain.com/folderone/foldertwo/index.php
加载正常的URL
从同一URL加载Zend Framework URL
www.mydomain.com/folderone(controller)/folder2(action)/variables
我该如何创造这种逻辑?
我错过了什么?
我真的致力于创建这个框架。
我和我设置框架的任务相同。 这对我来说是一个解决方案。
首先创建.htaccess文件。 设置重写条件并排除模板路径。 你可以这样做(只需复制和粘贴):
RewriteEngine On
Options +Indexes
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/index\.php -f
RewriteCond %{DOCUMENT_ROOT}/template -d
RewriteRule ^(.*)$ index\.php?$1 [QSA]
在我开始之前,我们必须至少创建五个目录:
/var/www/models/
/var/www/controllers/
/var/www/classes/
/var/www/system/
/var/www/template/
现在,我在index.php中添加了一个自动加载:
<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
function autoload($class_name)
{
$autoloadDirs = array('models', 'classes', 'controllers');
foreach($autoloadDirs as $dir)
{
if(file_exists($dir.'/'.$class_name.'.php'))
{
require_once($dir.'/'.$class_name.'.php');
}
}
}
spl_autoload_register('autoload');
require_once('system/Calling.php');
Calling::Run();
?>
在上面的脚本中,您将在Calling.php中看到require_once
class Calling
{
public static function Run($querystring = null)
{
//1. Parameter = Contollername
//2. Parameter = Action
$qString = preg_replace('/(\/$|^\/)/','',$querystring === null ? $_SERVER['QUERY_STRING'] : $querystring);
$callParam = !empty($qString) ? explode('/', $qString) : array();
$controllerName = count($callParam) > 0 ? (class_exists(ucfirst($callParam[0]).'Controller') ? ucfirst(array_shift($callParam)) : 'Error') : 'Main';
//All controllers have suffix "Controller" -> NameController.php
//and class name ike NameController
//If no controller name given, use MainController.php
$controllerClassName = $controllerName.'Controller';
//All public methods have suffix "Action" -> myMethodAction
//If there is no method named, use DefaultAction
$actionName = count($callParam) > 0 && method_exists($controllerClassName, ucfirst($callParam[0]).'Action') ? ucfirst(array_shift($callParam)) : 'Default';
$actionFunctionName = $actionName.'Action';
//Fetch the params
$param = new stdClass();
for($i = 0; $i < count($callParam); $i += 2)
{
$param->{$callParam[$i]} = isset($callParam[$i + 1]) ? $callParam[$i+1] : null;
}
////////////////////////////////////////////////////////////
//Init the Controller
$controller = new $controllerClassName($controllerName, $actionName);
$controller->$actionFunctionName($param);
////////////////////////////////////////////////////////////
//If you adapt this code: Is up to you to extends your controller
//from an internal controller which has the method Display();
$controller->Display();
}
}
此外,在您的控制器目录中添加您的第一个控制器名称MainController.php
//--> just better if you have also an internal controller with your global stuff
//--> class MainController extends Controller
class MainController
{
/** This is the default action
* @param $params
* @access public
* @return
*/
public function DefaultAction(stdClass $params)
{
//-> Do your staff here
}
/** This is the second action
* @param $params
* @access public
* @return
*/
public function SecondAction(stdClass $params)
{
//-> Do your staff here
}
/** This is the view handling method which has to run at least
* and I recommend to set up an internal controller and to extend
* all other controller from it and include this method in your
* internal controller
* @param
* @access
* @return
*/
public function Display()
{
//-> Run your template here
}
?>
现在,您可以像这样调用控制器,方法和参数:
//-> Load the main controller with default action
www.example.com/
//-> Load the main controller with default action
www.example.com/main/default/
//-> Load the main controller with second action
www.example.com/main/second/
//-> Load the main controller with second action and gives two params
www.example.com/main/second/key1/value1/key2/value2/
现在,您将拥有以下文件和目录来启动您自己的框架。
/var/www/.htaccess
/var/www/index.php
/var/www/controllers/MainController.php
/var/www/system/Calling.php
/var/www/models/
/var/www/classes/
/var/www/template/
享受您的新基本框架套件
Zend FrameWork和大多数MVC框架使用BootStrap。
它通过使用类似的东西将URL(使用.htaccess)路由到一个文件,例如(index.php):
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
它加载引导程序和路由类,从URL中获取所需的任何数据。
根据ZEND手册:
路由是获取URI端点(位于基本URL之后的URI的一部分)并将其分解为参数以确定该控制器的哪个模块,控制器和操作应该接收请求的过程。 这个值的模块,控制器,动作和其他参数。 路由仅发生一次:最初收到请求时以及调度第一个控制器之前。
编辑:回答你的评论:远离ZEND FRAMEWORK,这是一个在新的PHP文件中测试的代码:
<?php
$url = 'http://test.com/test1/test2/news.php';
$parse = parse_url($url);
$tokens = explode("/", $parse[path]);
print_r($tokens);
?>
如果你想构建一个路由器,你可以看一下Glue PHP给你的例子。 这是一个只做路由部分的微框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.