Comments: on this page. Click to read or post your own.

Class Director

Description

Director is responsible for processing URLs, and providing environment information.

The most important part of director is Director::direct(), which is passed a URL and will execute the appropriate controller.

Director also has a number of static methods that provide information about the environment, such as Director::set_environment_type().

Located in /sapphire/core/control/Director.php (line 14)


	
			
Variable Summary
static mixed $alternateBaseFolder
static mixed $alternateBaseURL
static mixed $callbacks
static mixed $dev_servers
static mixed $environment_type
static mixed $siteMode
static mixed $test_servers
Method Summary
static void absoluteBaseURL ()
static void absoluteBaseURLWithAuth ()
static void absoluteURL ( $url, [ $relativeToSiteBase = false])
static void addRules ($priority $priority,  $rules)
static void add_callback ($function $function, [$mode $mode = 'site'])
static void baseFolder ()
static void baseURL ()
static void currentPage ()
static void direct ($url $url)
static void fileExists ($file $file)
static void forceSSL ()
static void forceWWW ()
static string getAbsFile (string $file)
static string get_environment_type ()
static string get_site_mode ()
static void get_status_code ()
static HTTPResponse|string handleRequest (HTTPRequest $request, Session $session)
static void history ([pagesBack $pagesBack = 1])
static void isDev ()
static void isLive ()
static void isTest ()
static bool is_absolute (string $path)
static boolean is_absolute_url (string $url)
static boolean is_ajax ()
static boolean is_cli ()
static boolean is_relative_url (string $url)
static boolean is_site_url (string $url)
static void link ()
static string makeRelative (string $url)
static boolean|string protocolAndHost ()
static void redirect ( $url, [ $code = 302])
static void redirectBack ()
static string redirected_to ()
static void setBaseFolder ( $baseFolder)
static void setBaseURL ( $baseURL)
static void set_dev_servers ($servers $servers)
static void set_environment_type ($et $et)
static void set_site_mode (string $mode)
static void set_status_code ( $code)
static void set_test_servers ($servers $servers)
static HTTPResponse test (string $url, [array $postVars = null], [Session $session = null], [string $httpMethod = null], [string $body = null], [array $headers = null])
static void urlParam ( $name)
static void urlParams ()
Director __construct ()
Variables
static mixed $alternateBaseFolder (line 24)
static mixed $alternateBaseURL (line 26)
static mixed $callbacks (line 41)

Sets the site mode (if it is the public site or the cms), and runs registered modules.

  • access: protected
static mixed $dev_servers = array(
'localhost',
'127.0.0.1'
)
(line 28)
static mixed $environment_type (line 35)
  • access: protected
static mixed $siteMode (line 22)
static mixed $test_servers = array() (line 33)
Methods
static method absoluteBaseURL (line 555)

Returns the Absolute URL of the site root.

static void absoluteBaseURL ()
static method absoluteBaseURLWithAuth (line 562)

Returns the Absolute URL of the site root, embedding the current basic-auth credentials into the URL.

static void absoluteBaseURLWithAuth ()
static method absoluteURL (line 316)

Turns the given URL into an absolute URL.

  • todo: Document how relativeToSiteBase works
static void absoluteURL ( $url, [ $relativeToSiteBase = false])
  • $url
  • $relativeToSiteBase
static method addRules (line 67)

Add URL matching rules to the Director.

The director is responsible for turning URLs into Controller objects. It does thi

static void addRules ($priority $priority,  $rules)
  • $priority $priority: The priority of the rules; higher values will get your rule checked first. We recommend priority 100 for your site's rules. The built-in rules are priority 10, standard modules are priority 50.
  • $rules
static method add_callback (line 669)

Allows a module to register with the director to be run once

the controller is instantiated. The optional 'mode' parameter can be either 'site' or 'cms', as those are the two values currently set by controllers. The callback function will be run at the initialization of the relevant controller.

static void add_callback ($function $function, [$mode $mode = 'site'])
  • $function $function: string PHP-function array based on http://php.net/call_user_func
  • $mode $mode: string
static method baseFolder (line 435)

Returns the root filesystem folder for the site.

It will be automatically calculated unless it is overridden with setBaseFolder().

static void baseFolder ()
static method baseURL (line 411)

Returns the root URL for the site.

It will be automatically calculated unless it is overridden with setBaseURL().

static void baseURL ()
static method currentPage (line 302)

Returns the dataobject of the current page.

This will only return a value if you are looking at a SiteTree page

static void currentPage ()
static method direct (line 90)

Process the given URL, creating the appropriate controller and executing it.

Request processing is handled as folows:

  • Director::direct() creates a new HTTPResponse object and passes this to Director::handleRequest().
  • Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle this request.
  • Controller::handleRequest($request) is then called. This will find a rule to handle the URL, and call the rule handling method.
  • RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a RequestHandler object.
In addition to request processing, Director will manage the session, and perform the output of the actual response to the browser.

  • uses: Director::handleRequest() - rule-lookup logic is handled by this.
  • uses: Controller::run() - Controller::run() handles the page logic for a Director::direct() call.
static void direct ($url $url)
  • $url $url: String, the URL the user is visiting, without the querystring.
static method fileExists (line 546)

Returns true if the given file exists.

static void fileExists ($file $file)
  • $file $file: Filename specified relative to the site root
static method forceSSL (line 580)

Force the site to run on SSL. To use, call from _config.php.

For example:

static void forceSSL ()
static method forceWWW (line 592)

Force a redirect to a domain starting with "www."

static void forceWWW ()
static method getAbsFile (line 538)

Given a filesystem reference relative to the site root, return the full file-system path.

  • access: public
static string getAbsFile (string $file)
  • string $file
static method get_environment_type (line 717)

Can also be checked with Director::isDev(), Director::isTest(), and Director::isLive().

  • return: 'dev', 'test' or 'live'
static string get_environment_type ()
static method get_site_mode (line 655)
static string get_site_mode ()
static method get_status_code (line 387)

Returns the current HTTP status code

static void get_status_code ()
static method handleRequest (line 233)

Handle an HTTP request, defined with a HTTPRequest object.

static HTTPResponse|string handleRequest (HTTPRequest $request, Session $session)
static method history (line 54)

Return a URL from this user's navigation history.

static void history ([pagesBack $pagesBack = 1])
  • pagesBack $pagesBack: The number of pages back to go. The default, 1, returns the previous page.
static method isDev (line 759)

This function will return true if the site is in a development environment.

For information about environment types, see Director::set_environment_type().

static void isDev ()
static method isLive (line 751)
static void isLive ()
static method isTest (line 792)

This function will return true if the site is in a test environment.

For information about environment types, see Director::set_environment_type().

static void isTest ()
static method is_absolute (line 487)

Returns true if a given path is absolute. Works under both *nix and windows systems

  • access: public
static bool is_absolute (string $path)
  • string $path
static method is_absolute_url (line 498)

Checks if a given URL is absolute (e.g. starts with 'http://' etc.).

  • access: public
static boolean is_absolute_url (string $url)
  • string $url
static method is_ajax (line 612)

Checks if the current HTTP-Request is an "Ajax-Request" by checking for a custom header set by prototype.js or wether a manually set request-parameter 'ajax' is present.

static boolean is_ajax ()
static method is_cli (line 628)

Returns true if this script is being run from the command line rather than the webserver.

  • access: public
static boolean is_cli ()
static method is_relative_url (line 513)

Checks if a given URL is relative by checking is_absolute_url().

  • access: public
static boolean is_relative_url (string $url)
  • string $url
static method is_site_url (line 527)

Checks if the given URL is belonging to this "site", as defined by makeRelative() and absoluteBaseUrl().

Useful to check before redirecting based on a URL from user submissions through $_GET or $_POST, and avoid phishing attacks by redirecting to an attackers server.

  • access: public
static boolean is_site_url (string $url)
  • string $url
static method link (line 402)

Returns a URL to composed of the given segments - usually controller, action, parameter

  • deprecated: 2.3 Use Controller::join_links()
static void link ()
static method makeRelative (line 457)

Turns an absolute URL or folder into one that's relative to the root of the site.

This is useful when turning a URL into a filesystem reference, or vice versa.

  • return: Either a relative URL if the checks succeeded, or the original (possibly absolute) URL.
  • todo: Implement checking across http/https protocols
static string makeRelative (string $url)
  • string $url: Accepts both a URL or a filesystem path
static method protocolAndHost (line 333)

Returns the part of the URL, 'http://www.mysite.com'.

  • return: The domain from the PHP environment. Returns FALSE is this environment variable isn't set.
static boolean|string protocolAndHost ()
static method redirect (line 365)

Redirect to another page.

  • $url can be an absolute URL
  • or it can be a URL relative to the "site base"
  • if it is just a word without an slashes, then it redirects to another action on the current controller.

static void redirect ( $url, [ $code = 302])
  • $url
  • $code
static method redirectBack (line 394)
  • deprecated: 2.3 Use Controller->redirectBack()
static void redirectBack ()
static method redirected_to (line 373)

Tests whether a redirection has been requested.

  • return: If redirect() has been called, it will return the URL redirected to. Otherwise, it will return null;
static string redirected_to ()
static method setBaseFolder (line 444)

Sets the root folder for the website.

If the site isn't accessible from the folder you provide, weird things will happen.

static void setBaseFolder ( $baseFolder)
  • $baseFolder
static method setBaseURL (line 427)

Sets the root URL for the website.

If the site isn't accessible from the URL you provide, weird things will happen.

static void setBaseURL ( $baseURL)
  • $baseURL
static method set_dev_servers (line 734)

Specify HTTP_HOST values that are development environments.

For information about environment types, see Director::set_environment_type().

static void set_dev_servers ($servers $servers)
  • $servers $servers: array An array of HTTP_HOST values that should be treated as development environments.
static method set_environment_type (line 703)

Set the environment type of the current site.

Typically, a SilverStripe site have a number of environments:

  • development environments, such a copy on your local machine.
  • test sites, such as the one you show the client before going live.
  • the live site itself.
The behaviour of these environments often varies slightly. For example, development sites may have errors dumped to the screen, and order confirmation emails might be sent to the developer instead of the client.

To help with this, Sapphire support the notion of an environment type. The environment type can be dev, test, or live.

You can set it explicitly with Director::set_environment_tpye(). Or you can use Director::set_dev_servers() and Director::set_test_servers() to set it implicitly, based on the value of $_SERVER['HTTP_HOST']. If the HTTP_HOST value is one of the servers listed, then the environment type will be test or dev. Otherwise, the environment type will be live.

Dev mode can also be forced by putting ?isDev=1 in your URL, which will ask you to log in and then push the site into dev mode for the remainder of the session. Putting ?isDev=0 onto the URL can turn it back. Generally speaking, these methods will be called from your _config.php file.

Once the environment type is set, it can be checked with Director::isDev(), Director::isTest(), and Director::isLive().

static void set_environment_type ($et $et)
  • $et $et: string The environment type: dev, test, or live.
static method set_site_mode (line 642)

Sets the site mode (if it is the public site or the cms), and runs registered modules.

static void set_site_mode (string $mode)
  • string $mode: 'site' or 'cms'
static method set_status_code (line 380)

Sets the HTTP status code

static void set_status_code ( $code)
  • $code
static method set_test_servers (line 743)

Specify HTTP_HOST values that are test environments.

For information about environment types, see Director::set_environment_type().

static void set_test_servers ($servers $servers)
  • $servers $servers: array An array of HTTP_HOST values that should be treated as test environments.
static method test (line 169)

Test a URL request, returning a response object.

This method is the counterpart of Director::direct() that is used in functional testing. It will execute the URL given,

  • usedby: TestSession::post()
  • usedby: TestSession::get()
  • uses: Controller::run() - Controller::run() handles the page logic for a Director::direct() call.
  • uses: getControllerForURL() - The rule-lookup logic is handled by this.
static HTTPResponse test (string $url, [array $postVars = null], [Session $session = null], [string $httpMethod = null], [string $body = null], [array $headers = null])
  • string $url: The URL to visit
  • array $postVars: The $_POST & $_FILES variables
  • Session $session: The Session object representing the current session. By passing the same object to multiple calls of Director::test(), you can simulate a peristed session.
  • string $httpMethod: The HTTP method, such as GET or POST. It will default to POST if postVars is set, GET otherwise. Overwritten by $postVars['_method'] if present.
  • string $body: The HTTP body
  • array $headers: HTTP headers with key-value pairs
static method urlParam (line 287)

Returns the urlParam with the given name

static void urlParam ( $name)
  • $name
static method urlParams (line 294)

Returns an array of urlParams

static void urlParams ()
Constructor __construct (line 43)
Director __construct ()
blog comments powered by Disqus

Documentation generated on Mon, 23 Mar 2009 06:49:08 +1300 by phpDocumentor 1.3.2