很多站长朋友们都不太清楚php执行router,今天小编就来给大家整理php执行router,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php怎样不使用框架的情况下本地模拟url路由,实现localhost/a/id/1这种的访问方式 2、 php自带服务器命令php -S ..怎么让它在后台运行 3、 【PHP】laravel中获取当前路由名称 php怎样不使用框架的情况下本地模拟url路由,实现localhost/a/id/1这种的访问方式要实现路由的话你依然需要框架中路由器的支持,因为服务器不能理解你路径的具体含义.所以你需要一个路由器来帮助服务器去处理特定的信息.
不想用现成的就自己写一个简单的,如下:
首先你需要在htdoc下放一个.htaccess来实现单文件入口:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php?_url= [QSA,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
</IfModule>
然后自己写路由咯, index.php
<?php
//这里添加你想要的路径
$route = array(
//(:num)表示匹配任何数字,(:any)表示任意字符
'a/id/(:num)' => 'TestController:idAction',
'a/any/(:any)' => 'TestController:anyAction',
'a/no' => 'TestController:noAction',
//这里是默认控制器,就是当你访问localhost的时候用
'_DEFAULT_' => 'IndexController:indexAction',
);
//简单的Router
class Router
{
private $route;
public function __construct(array $route)
{
$this->route = $route;
}
public function parse($url)
{
if(empty($url)) {
list($controller, $action) = explode(':', $this->route['_DEFAULT_']);
return array(
'controller' => $controller,
'action' => $action,
'params' => array(),
);
}
$trans = array(
':any' => '[^/]+',
':num' => '[0-9]+'
);
foreach($this->route as $u => $d) {
$pattern = '#^' . strtr($u, $trans) . '$#';
if(preg_match($pattern, $url, $params)) {
list($controller, $action) = explode(':', $d);
array_shift($params);
return array(
'controller' => $controller,
'action' => $action,
'params' => $params,
);
}
}
header("HTTP/1.0 404 Not Found");
exit('Page not found');
}
}
$r = new Router($route);
$arr = $r->parse($_GET['_url']);
require($arr['controller'] . '.php');
//执行控制器的功能
$dispatcher = new $arr['controller'];
call_user_func_array(array($dispatcher, $arr['action']), $arr['params']);
?>
控制器1. Testcontroller.php
<?php
class TestController
{
public function idAction($id)
{
echo "Your int-only id is: {$id}";
}
public function anyAction($any_id)
{
echo "You any id is: {$any_id}";
}
public function noAction()
{
echo "This method take no parameter";
}
}
默认控制器: IndexController.php
<?php
class IndexController
{
public function indexAction()
{
echo "Hello World!";
}
}
把.htaccess, index.php, TestController.php, IndexController.php放在htdoc里就可以了
php自带服务器命令php -S ..怎么让它在后台运行当你在命令行启动这个Web Server时,如果指定了一个PHP文件,则这个文件会作为一个“路由”脚本,意味着每次请求都会先执行这个脚本。如果这个脚本返回 FALSE ,那么直接返回请求的文件(例如请求静态文件不作任何处理)。否则会把输出返回到浏览器。
Example #1 启动Web服务器 服务于当前目录
$ php -S localhost:8000
Example #2 启动时指定根目录
$ php -S localhost:8000 -t foo/
Example #3 使用路由(Router)脚本
$ php -S localhost:8000 router.php
【PHP】laravel中获取当前路由名称结论:Route类是必须引入的。所以可以调用静态方法 currentRouteName()。
1 - 为什么不引入Route类也可以运行?
因为Route类是在系统启动时作为全局类进行了注册。
在文件 config/app.php 文件内如下所示:
Route门面此处注册到全局,也就是根命名空间。所以在程序内,直接使用 Route::method() 不会有任何问题。
我们在编程中,对于全局注册的类,也需要通过此方法,添加注册。
2 - 获取当前路由名称的一些方法举例
使用Route类的方法:
Route::getCurrentRoute()->getPath();
或者使用Request类的方法:
\Request::route()->getName();
laravel 5.1 你得这么写:
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
到了5.2版本,就是题主的写法:
Route::currentRouteName();
5.3版本到5.8版本,更加灵活了:
$uri = $request->path();
使用 Request 对象的方法就可以返回。获取路由,路由名称,方法名:
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
到了laravel 7.x 对请求对象 Request 有了更加丰富的特性:
$request->route()->getName();
结语
以上获取路由名的方法,根据不同laravel版本,进行不同的处理。
细节上的不同一定要多加注意。
关于php执行router的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php执行router php执行js的详细内容...