在php中创建自己的引导
我正在尝试用php创建引导文件。 php文件是我在根目录下的引导文件在php中创建自己的引导,php,.htaccess,bootstrapping,Php,.htaccess,Bootstrapping,我正在尝试用php创建引导文件。 php文件是我在根目录下的引导文件 define('ROOT',getcwd().DIRECTORY_SEPARATOR); function url(){ return sprintf( "%s://%s%s", isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['HTTP_HOST'], $
define('ROOT',getcwd().DIRECTORY_SEPARATOR);
function url(){
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
);
}
在这里,我有上面的代码来获取所有php文件中的根目录和基本url。所以,对于所有请求,我都有.htaccess文件重定向到index.php
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
我已经创建了用于测试引导的php文件(test.php)
这里,我想通过index.php重定向所有请求。在本例中,我想在上面的url test.php中打印url()的返回值
这有什么问题,我创建引导正确吗
<?php
include('bootstrap.php');
echo ROOT." ".url();
?>
test.php
<?php
echo ROOT." ".url();
?>
及
(由于
RewriteCond%{REQUEST_FILENAME}!-f
)看起来cske已经有了解决方案,但我只想补充一点,为了调试这种事情并解决未来的问题,您可以执行以下操作之一:
RewriteCond%{REQUEST\u FILENAME}-f
表示请求的资源不是一个文件,但test.php实际上是一个文件,它将加载它
如果您想实现控制器、视图等,请从根目录中删除测试文件,并将其放置在可通过index.php访问的位置
index.php
define('ROOT',getcwd().DIRECTORY_SEPARATOR);
function url(){
return sprintf(
"%s://%s%s",
isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
);
}
include('test.php');
define('ROOT',getcwd().DIRECTORY_SEPARATOR);
function takeAction($className,$method,$args=array()){
$path="protected/class.{$className}.php";
if(file_exists($path)){
include_once($path);
$instance=new $className();
$instance->$method($args);
}else
throw new Exception('Invalid resource .... ');
}
$className=isset($_GET['className']) ? $_GET['className'] : 'defaultClass';
$action=isset($_GET['action']) ? $_GET['action'] : 'index';
$params=$_GET; //
takeAction($className,$action,$params);
你应该有一些惯例来翻译你的url,例如
http://www.mydomain.com/[className]/[action]/[parameter]
最好的教程是
当然,这不是经过测试/工作的代码创建引导,你说。Bootstrap是一个由多个文件组成的CSS库,“创建”它毫无意义。你需要澄清一下。@Ariane,这是推特引导。引导只是应用程序启动时的一系列事件。@FDL哦,这就是它的意思。我已经看到了很多引导,我完全忘记了这个词本身。我很尴尬,我想你不明白我的问题。这里,index.php是引导文件。我想将所有请求重定向到单个文件index.php,以便获得基本url的值。同样,我可以在引导文件中包含数据库连接代码和其他代码。致命错误:调用未定义的函数url()。实际上,这里我想要的是自动将index.php的所有代码加载到所有请求php文件中,而不使用该文件中的include或require函数。
define('ROOT',getcwd().DIRECTORY_SEPARATOR);
function takeAction($className,$method,$args=array()){
$path="protected/class.{$className}.php";
if(file_exists($path)){
include_once($path);
$instance=new $className();
$instance->$method($args);
}else
throw new Exception('Invalid resource .... ');
}
$className=isset($_GET['className']) ? $_GET['className'] : 'defaultClass';
$action=isset($_GET['action']) ? $_GET['action'] : 'index';
$params=$_GET; //
takeAction($className,$action,$params);
http://www.mydomain.com/[className]/[action]/[parameter]