lithium\net\http\Media::asset PHP Méthode

asset() public static méthode

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.
Résultat 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.
    public static function asset($path, $type, array $options = array())
    {
        $options = static::_assetOptions($path, $type, $options);
        $params = compact('path', 'type', 'options');
        return static::_filter(__FUNCTION__, $params, function ($self, $params) {
            $path = $params['path'];
            $type = $params['type'];
            $options = $params['options'];
            $library = $options['library'];
            if (preg_match('/^(?:[a-z0-9-]+:)?\\/\\//i', $path)) {
                return $path;
            }
            $config = Libraries::get($library);
            $paths = $options['paths'];
            $config['default'] ? end($paths) : reset($paths);
            $options['library'] = basename($config['path']);
            if ($options['suffix'] && strpos($path, $options['suffix']) === false) {
                $path .= $options['suffix'];
            }
            return $self::filterAssetPath($path, $paths, $config, compact('type') + $options);
        });
    }

Usage Example

Exemple #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);
 }
All Usage Examples Of lithium\net\http\Media::asset