本文整理汇总了PHP中SS_HTTPRequest::match方法的典型用法代码示例。如果您正苦于以下问题:PHP SS_HTTPRequest::match方法的具体用法?PHP SS_HTTPRequest::match怎么用?PHP SS_HTTPRequest::match使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SS_HTTPRequest
的用法示例。
在下文中一共展示了SS_HTTPRequest::match方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testMatch
public function testMatch()
{
$request = new SS_HTTPRequest("GET", "admin/crm/add");
/* When a rule matches, but has no variables, array("_matched" => true) is returned. */
$this->assertEquals(array("_matched" => true), $request->match('admin/crm', true));
/* Becasue we shifted admin/crm off the stack, just "add" should be remaining */
$this->assertEquals("add", $request->remaining());
$this->assertEquals(array("_matched" => true), $request->match('add', true));
}
示例2: getHTTPRequest
protected function getHTTPRequest($method = 'GET', $class = 'ApiTest_Book', $id = '', $params = array())
{
$request = new SS_HTTPRequest($method, 'api/' . $class . '/' . $id, $params);
$request->match($this->url_pattern);
$request->setRouteParams(array('Controller' => 'RESTfulAPI'));
return $request;
}
示例3: preview
/**
* @param SS_HTTPRequest $request
*
* @return string|HTMLText
*/
public function preview(SS_HTTPRequest $request)
{
$key = $request->param('Key');
$token = $request->param('Token');
/**
* @var ShareToken $shareToken
*/
$shareToken = ShareToken::get()->filter('token', $token)->first();
if (!$shareToken) {
return $this->errorPage();
}
$page = Versioned::get_one_by_stage('SiteTree', 'Stage', sprintf('"SiteTree"."ID" = \'%d\'', $shareToken->PageID));
$latest = Versioned::get_latest_version('SiteTree', $shareToken->PageID);
$controller = $this->getControllerFor($page);
if (!$shareToken->isExpired() && $page->generateKey($shareToken->Token) === $key) {
Requirements::css(SHAREDRAFTCONTENT_DIR . '/css/top-bar.css');
// Temporarily un-secure the draft site and switch to draft
$oldSecured = Session::get('unsecuredDraftSite');
$oldMode = Versioned::get_reading_mode();
$restore = function () use($oldSecured, $oldMode) {
Session::set('unsecuredDraftSite', $oldSecured);
Versioned::set_reading_mode($oldMode);
};
// Process page inside an unsecured draft container
try {
Session::set('unsecuredDraftSite', true);
Versioned::reading_stage('Stage');
// Create mock request; Simplify request to single top level reqest
$pageRequest = new SS_HTTPRequest('GET', $page->URLSegment);
$pageRequest->match('$URLSegment//$Action/$ID/$OtherID', true);
$rendered = $controller->handleRequest($pageRequest, $this->model);
// Render draft heading
$data = new ArrayData(array('Page' => $page, 'Latest' => $latest));
$include = (string) $data->renderWith('Includes/TopBar');
} catch (Exception $ex) {
$restore();
throw $ex;
}
$restore();
return str_replace('</body>', $include . '</body>', (string) $rendered->getBody());
} else {
return $this->errorPage();
}
}
示例4: handleRequest
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
{
self::$is_at_root = true;
$this->setDataModel($model);
$this->pushCurrent();
$this->init();
if (!($site = Multisites::inst()->getCurrentSiteId())) {
return $this->httpError(404);
}
$page = SiteTree::get()->filter(array('ParentID' => $site, 'URLSegment' => 'home'));
if (!($page = $page->first())) {
return $this->httpError(404);
}
$request = new SS_HTTPRequest($request->httpMethod(), $page->RelativeLink(), $request->getVars(), $request->postVars());
$request->match('$URLSegment//$Action', true);
$front = new MultisitesFrontController();
$response = $front->handleRequest($request, $model);
$this->popCurrent();
return $response;
}
开发者ID:helpfulrobot,项目名称:sheadawson-silverstripe-multisites,代码行数:20,代码来源:MultisitesRootController.php
示例5: handleRequest
public function handleRequest(SS_HTTPRequest $request, DataModel $model)
{
// If this is the final portion of the request (i.e. the URL is just /admin), direct to the default panel
if ($request->allParsed()) {
$base = $this->config()->url_base;
$segment = Config::inst()->get($this->config()->default_panel, 'url_segment');
$this->redirect(Controller::join_links($base, $segment));
return $this->getResponse();
} else {
$rules = self::rules();
foreach ($rules as $pattern => $controller) {
if (($arguments = $request->match($pattern, true)) !== false) {
$controllerObj = Injector::inst()->create($controller);
$controllerObj->setSession($this->session);
return $controllerObj->handleRequest($request, $model);
}
}
}
return $this->httpError(404, 'Not found');
}
示例6: routeRequest
function routeRequest(SS_HTTPRequest $request)
{
// Handle the routing
$noun = singleton('RESTRoot');
while (!$request->allParsed()) {
$matched = false;
if ($params = $request->match('$Next!', true)) {
$matched = true;
$next = $params['Next'];
try {
if (method_exists($noun, 'getItem')) {
$noun = $noun->getItem($next);
} else {
$noun = $noun->{$next};
}
} catch (Exception $e) {
if ($e instanceof SS_HTTPResponse_Exception) {
throw $e;
} elseif ($e instanceof RESTException) {
$handler = $this->getHandler($noun);
$handler->respondWithError(array('code' => $e->getCode(), 'exception' => $e));
} else {
$handler = $this->getHandler($noun);
$handler->respondWithError(array('code' => 500, 'exception' => $e));
}
}
if (!$noun) {
$this->httpError(404);
}
}
if (!$matched) {
$this->httpError(404);
}
}
// Find the handler and call
$handler = $this->getHandler($noun);
return $handler->handleRequest($request);
}
示例7: handleRequest
/**
* Handles URL requests.
*
* - ViewableData::handleRequest() iterates through each rule in {@link self::$url_handlers}.
* - If the rule matches, the named method will be called.
* - If there is still more URL to be processed, then handleRequest()
* is called on the object that that method returns.
*
* Once all of the URL has been processed, the final result is returned.
* However, if the final result is an array, this
* array is interpreted as being additional template data to customise the
* 2nd to last result with, rather than an object
* in its own right. This is most frequently used when a Controller's
* action will return an array of data with which to
* customise the controller.
*
* @param $request The {@link SS_HTTPRequest} object that is reponsible for distributing URL parsing
* @uses SS_HTTPRequest
* @uses SS_HTTPRequest->match()
* @return SS_HTTPResponse|RequestHandler|string|array
*/
function handleRequest(SS_HTTPRequest $request, DataModel $model) {
// $handlerClass is used to step up the class hierarchy to implement url_handlers inheritance
$handlerClass = ($this->class) ? $this->class : get_class($this);
if($this->brokenOnConstruct) {
user_error("parent::__construct() needs to be called on {$handlerClass}::__construct()", E_USER_WARNING);
}
$this->request = $request;
$this->setModel($model);
// We stop after RequestHandler; in other words, at ViewableData
while($handlerClass && $handlerClass != 'ViewableData') {
$urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::FIRST_SET);
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
if(isset($_REQUEST['debug_request'])) Debug::message("Testing '$rule' with '" . $request->remaining() . "' on $this->class");
if($params = $request->match($rule, true)) {
// Backwards compatible setting of url parameters, please use SS_HTTPRequest->latestParam() instead
//Director::setUrlParams($request->latestParams());
if(isset($_REQUEST['debug_request'])) {
Debug::message("Rule '$rule' matched to action '$action' on $this->class. Latest request params: " . var_export($request->latestParams(), true));
}
// Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action',
if($action[0] == '$') $action = $params[substr($action,1)];
if($this->checkAccessAction($action)) {
if(!$action) {
if(isset($_REQUEST['debug_request'])) Debug::message("Action not set; using default action method name 'index'");
$action = "index";
} else if(!is_string($action)) {
user_error("Non-string method name: " . var_export($action, true), E_USER_ERROR);
}
try {
if(!$this->hasMethod($action)) {
return $this->httpError(404, "Action '$action' isn't available on class " . get_class($this) . ".");
}
$result = $this->$action($request);
} catch(SS_HTTPResponse_Exception $responseException) {
$result = $responseException->getResponse();
}
} else {
return $this->httpError(403, "Action '$action' isn't allowed on class " . get_class($this) . ".");
}
if($result instanceof SS_HTTPResponse && $result->isError()) {
if(isset($_REQUEST['debug_request'])) Debug::message("Rule resulted in HTTP error; breaking");
return $result;
}
// If we return a RequestHandler, call handleRequest() on that, even if there is no more URL to parse.
// It might have its own handler. However, we only do this if we haven't just parsed an empty rule ourselves,
// to prevent infinite loops. Also prevent further handling of controller actions which return themselves
// to avoid infinite loops.
if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result) && $result instanceof RequestHandler) {
$returnValue = $result->handleRequest($request, $model);
// Array results can be used to handle
if(is_array($returnValue)) $returnValue = $this->customise($returnValue);
return $returnValue;
// If we return some other data, and all the URL is parsed, then return that
} else if($request->allParsed()) {
return $result;
// But if we have more content on the URL and we don't know what to do with it, return an error.
} else {
return $this->httpError(404, "I can't handle sub-URLs of a $this->class object.");
}
return $this;
}
}
$handlerClass = get_parent_class($handlerClass);
//.........这里部分代码省略.........
示例8: handleRequest
/**
* Custom request handler that will check component handlers before proceeding to the default implementation.
*
* @todo There is too much code copied from RequestHandler here.
*/
function handleRequest(SS_HTTPRequest $request, DataModel $model) {
if($this->brokenOnConstruct) {
user_error("parent::__construct() needs to be called on {$handlerClass}::__construct()", E_USER_WARNING);
}
$this->request = $request;
$this->setModel($model);
$fieldData = $this->request->requestVar($this->getName());
if($fieldData && $fieldData['GridState']) $this->getState(false)->setValue($fieldData['GridState']);
foreach($this->components as $component) {
if(!($component instanceof GridField_URLHandler)) {
continue;
}
$urlHandlers = $component->getURLHandlers($this);
if($urlHandlers) foreach($urlHandlers as $rule => $action) {
if($params = $request->match($rule, true)) {
// Actions can reference URL parameters, eg, '$Action/$ID/$OtherID' => '$Action',
if($action[0] == '$') $action = $params[substr($action,1)];
if(!method_exists($component, 'checkAccessAction') || $component->checkAccessAction($action)) {
if(!$action) {
$action = "index";
} else if(!is_string($action)) {
throw new LogicException("Non-string method name: " . var_export($action, true));
}
try {
$result = $component->$action($this, $request);
} catch(SS_HTTPResponse_Exception $responseException) {
$result = $responseException->getResponse();
}
if($result instanceof SS_HTTPResponse && $result->isError()) {
return $result;
}
if($this !== $result && !$request->isEmptyPattern($rule) && is_object($result) && $result instanceof RequestHandler) {
$returnValue = $result->handleRequest($request, $model);
if(is_array($returnValue)) {
throw new LogicException("GridField_URLHandler handlers can't return arrays");
}
return $returnValue;
// If we return some other data, and all the URL is parsed, then return that
} else if($request->allParsed()) {
return $result;
// But if we have more content on the URL and we don't know what to do with it, return an error.
} else {
return $this->httpError(404, "I can't handle sub-URLs of a " . get_class($result) . " object.");
}
}
}
}
}
return parent::handleRequest($request, $model);
}
示例9: handleBulkAction
/**
* Pass control over to the RequestHandler
* loop through the handlers provided in config['actions']
* and find matching url_handlers.
*
* $url_handlers rule should not use wildcards like '$Action' => '$Action'
* but have more specific path defined
*
* @param GridField $gridField
* @param SS_HTTPRequest $request
*
* @return mixed
*/
public function handleBulkAction($gridField, $request)
{
$controller = $gridField->getForm()->Controller();
foreach ($this->config['actions'] as $name => $data) {
$handlerClass = $data['handler'];
$urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::UNINHERITED);
if ($urlHandlers) {
foreach ($urlHandlers as $rule => $action) {
if ($request->match($rule, false)) {
//print_r('matched ' . $handlerClass . ' to ' . $rule);
$handler = Injector::inst()->create($handlerClass, $gridField, $this, $controller);
return $handler->handleRequest($request, DataModel::inst());
}
}
}
}
user_error('Unable to find matching bulk action handler for ' . $request->remaining() . '.', E_USER_ERROR);
}
示例10: requestURL
/**
* Mock a request against a given controller
*
* @param ContentController $controller
* @param string $url
*/
protected function requestURL(ContentController $controller, $url)
{
$request = new SS_HTTPRequest('get', $url);
$request->match('$URLSegment//$Action/$ID/$OtherID');
$request->shift();
$controller->init();
$controller->handleRequest($request, new DataModel());
}
示例11: handleRequest
/**
* @param SS_HTTPRequest $request
* @return SS_HTTPResponse
*/
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null)
{
self::$is_at_root = true;
$this->setDataModel($model);
$this->pushCurrent();
$this->init();
if (!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
$this->response = new SS_HTTPResponse();
$this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
return $this->response;
}
$request = new SS_HTTPRequest($request->httpMethod(), self::get_homepage_link() . '/', $request->getVars(), $request->postVars());
$request->match('$URLSegment//$Action', true);
$controller = new ModelAsController();
$result = $controller->handleRequest($request, $model);
$this->popCurrent();
return $result;
}
示例12: createFakeHTTPRequest
function createFakeHTTPRequest($id)
{
$r = new SS_HTTPRequest('GET', 'index/' . $id);
$r->match('index/$ID');
return $r;
}
示例13: handleRequest
public function handleRequest(SS_HTTPRequest $request, DataModel $model)
{
$this->request = $request;
if (!$this->authenticate()) {
return $this->permissionFailure();
}
if (!$this->authorize()) {
return $this->permissionFailure();
}
$controller_class = $this->class ? $this->class : get_class($this);
$url_handlers = Config::inst()->get($controller_class, 'url_handlers', Config::FIRST_SET);
if ($url_handlers) {
foreach ($url_handlers as $rule => $action) {
if ($params = $request->match($rule)) {
$res = $this->doBeforeFilter($action, $params);
if ($res) {
return $res;
}
break;
}
}
}
return parent::handleRequest($request, $model);
}
示例14: handleRequest
/**
* Handle an HTTP request, defined with a SS_HTTPRequest object.
*
* @return SS_HTTPResponse|string
*/
protected static function handleRequest(SS_HTTPRequest $request, Session $session, DataModel $model)
{
$rules = Config::inst()->get('Director', 'rules');
if (isset($_REQUEST['debug'])) {
Debug::show($rules);
}
foreach ($rules as $pattern => $controllerOptions) {
if (is_string($controllerOptions)) {
if (substr($controllerOptions, 0, 2) == '->') {
$controllerOptions = array('Redirect' => substr($controllerOptions, 2));
} else {
$controllerOptions = array('Controller' => $controllerOptions);
}
}
if (($arguments = $request->match($pattern, true)) !== false) {
$request->setRouteParams($controllerOptions);
// controllerOptions provide some default arguments
$arguments = array_merge($controllerOptions, $arguments);
// Find the controller name
if (isset($arguments['Controller'])) {
$controller = $arguments['Controller'];
}
// Pop additional tokens from the tokeniser if necessary
if (isset($controllerOptions['_PopTokeniser'])) {
$request->shift($controllerOptions['_PopTokeniser']);
}
// Handle redirections
if (isset($arguments['Redirect'])) {
return "redirect:" . Director::absoluteURL($arguments['Redirect'], true);
} else {
Director::$urlParams = $arguments;
$controllerObj = Injector::inst()->create($controller);
$controllerObj->setSession($session);
try {
$result = $controllerObj->handleRequest($request, $model);
} catch (SS_HTTPResponse_Exception $responseException) {
$result = $responseException->getResponse();
}
if (!is_object($result) || $result instanceof SS_HTTPResponse) {
return $result;
}
user_error("Bad result from url " . $request->getURL() . " handled by " . get_class($controllerObj) . " controller: " . get_class($result), E_USER_WARNING);
}
}
}
/**
* @andrelohmann
*
* This code allows to return custom Error Pages without using the CMS Module
*
*/
$template = array('ErrorPage', 'Page');
$result = new ArrayData(array('Title' => 404, 'Content' => DBField::create_field('HTMLText', 'No URL rule was matched')));
// No URL rules matched, so return a 404 error.
return new SS_HTTPResponse($result->renderWith($template), 404);
/**
* Original Code
*
* // No URL rules matched, so return a 404 error.
* return new SS_HTTPResponse('No URL rule was matched', 404);
*
*/
}
开发者ID:helpfulrobot,项目名称:andrelohmann-silverstripe-framework-errorpage-patch,代码行数:68,代码来源:Director.php
示例15: handleRequest
public function handleRequest(SS_HTTPRequest $request, DataModel $model)
{
if ($request->match('addinlinerecord', true)) {
// NOTE(Jake): Handling here as I'm not sure how to do a url_handler that allows
// infinite parameters after 'addinlinerecord'
$result = $this->handleAddInline($request);
if ($result && is_object($result) && $result instanceof RequestHandler) {
// NOTE(Jake): Logic copied from parent::handleRequest()
$returnValue = $result->handleRequest($request, $model);
if ($returnValue && is_array($returnValue)) {
$returnValue = $this->customise($returnValue);
}
return $returnValue;
}
// NOTE(Jake): Consume all remaining parts so that 'RequestHandler::handleRequest'
// doesn't hit an error. (Use Case: Getting an error with a GridField::handleRequest)
// NOTE(Jake): THis is probably due to just CLASSNAME not being consumed/shifted in 'addinlinerecord'
// but cbf changing and re-testing everything.
$dirParts = explode('/', $request->remaining());
foreach ($dirParts as $dirPart) {
$request->shift();
}
return $result;
}
$result = parent::handleRequest($request, $model);
return $result;
}