我们有一个旧网站,其中包含自定义ACL和路由设置。我们正在慢慢地将代码库迁移到Laravel。
为了从旧站点访问页面,当前所有内容都通过名为Modules.php
的文件运行,modname指定应打开哪个页面。例如:/Modules.php?modname=hello/world
显示世界页面。
我怎么能伪造laravel的路线?我想从Modules.php启动Laravel并使用modname作为路由。
所以我应该可以这样做:Route::get('/hello/world', 'MyController@hello');
现在我只是覆盖$_SERVER
,但问题就是让基本网址成为问题。
如果有人希望将来这样做,我们就是这样做的:
$request = Request::createFromGlobals();
$request->server->set('REQUEST_URI', $_REQUEST['modname']);
$app->run($request);
在laravel 5中,您可以通过传递$request
输入内核(使用public / index.php作为参考)来完成等效操作
至于我们如何确定路线是否是一条laravel路线,我做了这个功能。您可以通过将json文件设置为二叉树来优化它,但我没有注意到性能问题,所以我没有打扰。
<?php
/**
* Checks if Laravel should handle the request.
*
* @param $route string The route used internally by Laravel. Ex: sss/events
* @return boolean Whether Laravel creates the response for this route
*/
function IsLaravelRoute($route)
{
// The json file is made by running `php artisan routes:dump` in Laravel
// We compile the routes for performance by defering booting Laravel
// only if we know for sure that it is needed
$routeFilePath = realpath(__DIR__.'/../laravel.json');
// If Laravel isn't installed on the site, it's obviously not a Laravel route
if ($routeFilePath === false) {
return false;
}
// An array of objects with details foreach Laravel route
$laravelRoutes = json_decode(file_get_contents($routeFilePath));
// Some info we'll need to verify the route adheres to a route pattern
$requestMethod = $_SERVER['REQUEST_METHOD'];
$route = trim($route, '/');
$segments = explode('/', $route);
$len = count($segments);
foreach($laravelRoutes as $laravelRoute) {
// Format the laravel route details in such a way we can compare
// it with the route being checked
$routeSegments = explode('/', $laravelRoute->uri);
$routeLen = count($routeSegments);
$optionalParamCount = preg_match_all('/(\{.*\?\})/', $laravelRoute->uri);
// These conditions must be true; otherwise, we skip this iteration.
$sameMethods = in_array($requestMethod, $laravelRoute->methods);
$sameSegmentCount = $len === $routeLen;
// See if optional params can meet us half way
$routeDiff = $routeLen - $len;
// Possibly a route, we'll verify that
// it is in fact one later if this is true
$potentialRoute = $optionalParamCount >= $routeDiff && $routeDiff >= 0;
$looksLegit = $sameMethods && ($sameSegmentCount || $potentialRoute);
if (!$looksLegit) {
continue; // Ain't nobody got time for that
}
// Basic requirements for elgibility is there -- now verify
for ($i = 0; $i < $len; $i++) {
// We can only validate concrete route segments;
// we have to give the benefit of the doubt to route parameters.
$validate = !preg_match('/\{.*\}/', $routeSegments[$i]);
if ($validate && $segments[$i] !== $routeSegments[$i]) {
// This route isn't valid, but let's check the next $laravelRoute
continue 2;
}
}
// This route is legit
return true;
}
// Looked through all the routes, but looks like it isn't a Laravel page :(
return false;
}
当然我们想使用命名路由,因此Laravel会生成新路由,但为了做到这一点,我们扩展了UrlGenerator。请原谅全局变量。
<?php namespace Focus\Core\Route;
/**
* When the time comes just remove this class
* and use the default Laravel UrlGenerator class
*/
class UrlGenerator extends \Illuminate\Routing\UrlGenerator
{
/**
* We build the URL that is needed. This will pass everything through
* Modules.php
*
* @param \Illuminate\Routing\Route $route
* @param array $parameters
* @param bool $absolute
* @return string
*/
protected function toRoute($route, array $parameters, $absolute)
{
global $FocusURL;
$modname = $this->replaceRouteParameters($route->uri(), $parameters);
if (sizeof($parameters) > 0) {
// Append query string onto existing
$queryString = $this->getRouteQueryString($parameters);
$queryString = '&'.substr($queryString, 1);
} else {
$queryString = '';
}
$url = '/Modules.php?modname='.$modname.$queryString;
return $absolute ? rtrim($FocusURL, '/').$url : ltrim($url, '/');
}
/**
* Generate a URL to an application asset.
*
* @param string $path
* @param bool $secure
* @return string
*/
public function asset($path, $secure = null)
{
global $FocusURL;
if ($this->isValidUrl($path)) return $path;
// Shortcut to access a package's assets
if (strpos($path, '@') === 0) {
$path = 'packages/focus/'.substr($path, 1);
}
$path = ltrim($path, '/');
$filePath = base_path().'/public/'.$path;
// Prevent loading old version of assets due to caching
if (file_exists($filePath)) {
$path .= '?'.filemtime($filePath);
}
return rtrim($FocusURL, '/').'/laravel/public/'.$path;
}
}
至于laravel.json文件是如何让IsLaravel函数检查的,我们制作了这个artisan命令,我们用gulp观察它的变化
<?php namespace Focus\Core\Command;
use Illuminate\Console\Command;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class RouteDumper extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'routes:dump';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Creates a file that can be included in old focus to find laravel routes without starting the entire framework each request.';
protected $router;
protected $routes;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(Router $router)
{
parent::__construct();
$this->router = $router;
$this->routes = $router->getRoutes();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$json = array();
foreach ($this->routes as $route) {
$json[] = array(
'methods' => $route->methods(),
'uri' => $route->uri()
);
}
$path = base_path().'/../laravel.json';
$fileContents = json_encode($json);
File::put($path, $fileContents);
//$this->call("generate:laroute");
$this->info('All routes have been dumped!');
}
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array();
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array();
}
}
答案 0 :(得分:8)
您可以使用Request::create
方法创建请求,类似这样的
// Get the url
$modname = "hello/world";
$request = Request::create($modname, 'GET', array());
return Route::dispatch($request);