Gets the physical path to the web assets (i.e. /webroot) directory of a library.
public static webroot ( string | boolean $library = true, string | boolean $scope = null ) : string | ||
$library | string | boolean | The name of the library for which to find the path, or `true` for the default library. |
$scope | string | boolean | The name of the to use to find the path. |
리턴 | string | Returns the physical path to the web assets directory. |
public function testGetLibraryWebroot() { $this->assertTrue(is_dir(Media::webroot(true))); $this->assertNull(Media::webroot('foobar')); Libraries::add('foobar', array('path' => __DIR__, 'webroot' => __DIR__)); $this->assertEqual(__DIR__, Media::webroot('foobar')); Libraries::remove('foobar'); }
public function testGetLibraryWebroot() { $this->assertNull(Media::webroot('foobar')); Libraries::add('foobar', array('path' => __DIR__, 'webroot' => __DIR__)); $this->assertEqual(__DIR__, Media::webroot('foobar')); Libraries::remove('foobar'); $resources = Libraries::get(true, 'resources'); $webroot = "{$resources}/media_test/webroot"; $this->skipIf(!is_writable($resources), "Cannot write test app to resources directory."); if (!is_dir($webroot)) { mkdir($webroot, 0777, true); } Libraries::add('media_test', array('path' => "{$resources}/media_test")); $this->assertFileExists(Media::webroot('media_test')); Libraries::remove('media_test'); rmdir($webroot); }
* $posts = Post::find('all'); * return $posts->to('json'); * }}} */ use lithium\util\Collection; Collection::formats('lithium\\net\\http\\Media'); /** * This filter is a convenience method which allows you to automatically route requests for static * assets stored within active plugins. For example, given a JavaScript file `bar.js` inside the * `li3_foo` plugin installed in an application, requests to `http://app/path/li3_foo/js/bar.js` * will be routed to `/path/to/app/libraries/plugins/li3_foo/webroot/js/bar.js` on the filesystem. * In production, it is recommended that you disable this filter in favor of symlinking each * plugin's `webroot` directory into your main application's `webroot` directory, or adding routing * rules in your web server's configuration. */ use lithium\action\Dispatcher; use lithium\action\Response; use lithium\net\http\Media; Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { list($library, $asset) = explode('/', $params['request']->url, 2) + array("", ""); if ($asset && ($path = Media::webroot($library)) && file_exists($file = "{$path}/{$asset}")) { return function () use($file) { $info = pathinfo($file); $media = Media::type($info['extension']); $content = (array) $media['content']; return new Response(array('headers' => array('Content-type' => reset($content)), 'body' => file_get_contents($file))); }; } return $chain->next($self, $params, $chain); }); Media::type('js', array('application/javascript', 'text/javascript'), array('view' => 'lithium\\template\\View', 'paths' => array('template' => '{:library}/views/{:controller}/{:template}.{:type}.php', 'layout' => '{:library}/views/layouts/{:layout}.{:type}.php', 'element' => array('{:library}/views/elements/{:template}.{:type}.php', '{:library}/views/elements/{:template}.html.php'))));
<?php use lithium\core\Libraries; use lithium\action\Dispatcher; use lithium\action\Response; use lithium\net\http\Media; use li3_less\core\Less; define('LI3_BOOTSTRAP_PATH', dirname(__DIR__)); /* * this filter allows automatic linking and loading of assets from `webroot` folder */ Dispatcher::applyFilter('_callable', function ($self, $params, $chain) { list($library, $asset) = explode('/', ltrim($params['request']->url, '/'), 2) + array("", ""); if ($asset && $library == 'li3_bootstrap' && ($path = Media::webroot($library)) && file_exists($file = "{$path}/{$asset}")) { return function () use($file) { $info = pathinfo($file); $media = Media::type($info['extension']); $content = (array) $media['content']; return new Response(array('headers' => array('Content-type' => reset($content)), 'body' => file_get_contents($file))); }; } return $chain->next($self, $params, $chain); }); /** * Here we check, if there is a library called `li3_less` * * If that is the case, we can directly work with the * less files, that is much more flexible. To get this * up and running, you need to add li3_less as a library * and load it _before_ the li3_bootstrap library like this: *
* prefix. Otherwise default `backend` backend routes prefix will be used. */ $backend = Libraries::get('li3_backend', 'urlPrefix'); $backend || ($backend = 'backend'); /** * Enable access to assets from installed libraries * This route expect all `lithium` libraries prefixed with `li3_` * * For example route `'/assets/backend/css/bootstrap.css'` is equivalent to: * `'/assets/li3_backend/css/bootstrap.css'` and will look for assets in library `webroot` dir. * * To speed up your application you can copy any asset to your public webroot. Just create same * directory structure as url and your asset will be loaded properly. */ Router::connect('/assets/{:library}/{:args}', array(), function ($request) { $file = Media::webroot('li3_' . $request->params['library']); $file .= '/' . join('/', $request->params['args']); if (file_exists($file)) { $info = pathinfo($file); $media = Media::type($info['extension']); $content = (array) $media['content']; $response = new Response(array('headers' => array('Content-type' => reset($content)), 'body' => file_get_contents($file))); $response->cache('+1 hour'); return $response; } throw new \lithium\action\DispatchException(); }); /** * Backend routing * `backend` prefix is prepended to `action` name. */