asset() public static method

Calculates the web-accessible path to a static asset, usually a JavaScript, CSS or image file.
See also: lithium\net\http\Media::$_assets
See also: lithium\action\Request::env()
public static asset ( string $path, string $type, array $options = [] ) : string
$path string The path to the asset, relative to the given `$type`s path and without a suffix. If the path contains a URI Scheme (eg. `http://`), no path munging will occur.
$type string The asset type. See `Media::$_assets` or `Media::assets()`.
$options array Contains setting for finding and handling the path, where the keys are the following: - `'base'`: The base URL of your application. Defaults to `null` for no base path. This is usually set with the return value of a call to `env('base')` on an instance of `lithium\action\Request`. - `'check'`: Check for the existence of the file before returning. Defaults to `false`. - `'filter'`: An array of key/value pairs representing simple string replacements to be done on a path once it is generated. - `'paths'`: An array of paths to search for the asset in. The paths should use `String::insert()` formatting. See `Media::$_assets` for more. - `suffix`: The suffix to attach to the path, generally a file extension. - `'timestamp'`: Appends the last modified time of the file to the path if `true`. Defaults to `false`. - `'library'`: The name of the library from which to load the asset. Defaults to `true`, for the default library.
return string Returns the publicly-accessible absolute path to the static asset. If checking for the asset's existence (`$options['check']`), returns `false` if it does not exist in your `/webroot` directory, or the `/webroot` directories of one of your included plugins.
Example #1
0
 /**
  * Tests that `Media::asset()` will not prepend path strings with the base application path if
  * it has already been prepended.
  *
  * @return void
  */
 public function testDuplicateBasePathCheck()
 {
     $result = Media::asset('/foo/bar/image.jpg', 'image', array('base' => '/bar'));
     $this->assertEqual('/bar/foo/bar/image.jpg', $result);
     $result = Media::asset('/foo/bar/image.jpg', 'image', array('base' => '/foo/bar'));
     $this->assertEqual('/foo/bar/image.jpg', $result);
     $result = Media::asset('foo/bar/image.jpg', 'image', array('base' => 'foo'));
     $this->assertEqual('foo/img/foo/bar/image.jpg', $result);
     $result = Media::asset('/foo/bar/image.jpg', 'image', array('base' => ''));
     $this->assertEqual('/foo/bar/image.jpg', $result);
 }
Example #2
0
 public function testScopeBase()
 {
     $result = Media::asset('style.css', 'css');
     $this->assertEqual('/css/style.css', $result);
     Media::attach(false, array('base' => 'lithium/app/webroot'));
     $result = Media::asset('style.css', 'css');
     $this->assertEqual('/lithium/app/webroot/css/style.css', $result);
 }
Example #3
0
 public function testQueryUndefinedAssetTypes()
 {
     $base = Media::path('index.php', 'generic');
     $result = Media::path('index.php', 'foo');
     $this->assertEqual($result, $base);
     $base = Media::asset('/bar', 'generic');
     $result = Media::asset('/bar', 'foo');
     $this->assertEqual($result, $base);
 }
Example #4
0
 /**
  * Tests that empty asset paths correctly return the base path for the asset type, and don't
  * generate notices or errors.
  */
 public function testEmptyAssetPaths()
 {
     $this->assertEqual('/img/', Media::asset('', 'image'));
     $this->assertEqual('/css/.css', Media::asset('', 'css'));
     $this->assertEqual('/js/.js', Media::asset('', 'js'));
     $this->assertEqual('/', Media::asset('', 'generic'));
 }
 public function testManualAssetPaths()
 {
     $result = Media::asset('/path/file', 'js', array('base' => '/base'));
     $expected = '/base/path/file.js';
     $this->assertEqual($expected, $result);
     $result = Media::asset('/foo/bar', 'js', array('base' => '/base', 'check' => true));
     $this->assertFalse($result);
     $result = Media::asset('/css/base', 'css', array('base' => '/base', 'check' => true));
     $expected = '/base/css/base.css';
     $this->assertEqual($expected, $result);
     $result = Media::asset('/css/base.css', 'css', array('base' => '/base', 'check' => true));
     $expected = '/base/css/base.css';
     $this->assertEqual($expected, $result);
     $result = Media::asset('/css/base.css?foo', 'css', array('base' => '/base', 'check' => true));
     $expected = '/base/css/base.css?foo';
     $this->assertEqual($expected, $result);
 }