Table of Content
#Base URI
Sometimes you may put your application at sub folder of a domain, Windwalker provides a uri data help you get base uri.
If our application located at http://domain.com/sites/windwalker/
and we open /flower/sakura
page, we may use this code to get uri data:
$uri = \Windwalker\Ioc::get('uri');
// Get Base path
echo $uri->get('current');
echo $uri->get('base.path');
echo $uri->get('base.full');
echo $uri->get('base.host');
echo $uri->get('media.path');
echo $uri->get('media.full');
echo $uri->get('route');
We will get:
current ---> http://domain.com/sites/windwalker/flower/sakura
base.path ---> /sites/windwalker/
base.full ---> http://domain.com/sites/windwalker/
base.host ---> http://domain.com
media.path ---> /sites/windwalker/media/
media.full ---> http://domain.com/sites/windwalker/media/
route ---> flower/sakura
These uri data can help our page links be strong whenever we put this application. Fo example, this code will build a full path of link:
echo $link = $uri->get('base.full') . 'romeo/and/juliet';
The output will be:
http://domain.com/sites/windwalker/romeo/and/juliet
#Use base.path
Use this code:
// We can also use array access
echo $uri['base.path'] . '/flower/sakura';
We'll get a uri start from root, so the relative path will not break.
/sites/windwalker/flower/sakura
#Use URI in View
View has already included uri object as a global variable.
<link href="<?php echo $data->uri['media.path']; ?>css/bootstrap.min.css">
The output will be:
<link href="/sites/windwalker/media/css/bootstrap.min.css">
#Build Route
Every route in Windwalker has a key, we called it route name or route resources, this name will help us quickly build route.
For example, this is the routing file:
flower_page:
pattern: /flower/page/(id)-(alias).html
controller: Flower\Controller\Page
Then we can build this route by Router::build()
use Windwalker\Core\Router;
// Note: don't use \Windwalker\Router\Router
echo Router::build('flower_page', array('id' => 25, 'alias' => 'foo-bar-baz'));
The output will be:
flower/page/25-foo-bar-baz.html
This is a very useful function that you can change roue name but won't worry of link will be broke.
#Build Package Route
If your routes is definded in a package, you must add package alias before route name, and separate by at (@
):
use Windwalker\Core\Router;
Router::build('flower@sakuras', array('page' => $page));
Or use PackageRouter, we can ignore package prefix, Package will auto add it:
$package = PackageHelper::getPackage('flower');
// No necessary to add 'flower:'
$package->router->build('sakuras', array('page' => $page));
// If you want to build rote in other package, you may re add package name
$package->router->build('user@login');
#Build for Http or Html
Default build()
method will not encode URL, so we can use this URL to redirect page, the buildHttp()
and http()
is an alias of build()
.
// flower/sakuras?foo=bar&baz=yoo
echo Router::buildHttp('flower@sakuras', ['foo' => 'bar', 'baz' => 'yoo']);
// OR
echo Router::http('flower@sakuras', ['foo' => 'bar', 'baz' => 'yoo']);
If we want to build a URL to print it in HTML, we must encode it, so we have to use buildHtml()
or html()
method.
// flower/sakuras?foo=bar&baz=yoo
echo Router::buildHtml('flower@sakuras', ['foo' => 'bar', 'baz' => 'yoo']);
// OR
echo Router::html('flower@sakuras', ['foo' => 'bar', 'baz' => 'yoo']);
Result:
flower/sakuras?foo=bar&baz=yoo
flower/sakuras?foo=bar&baz=yoo
#Relative or Absolute URL
Router has 3 mode, RAW
, PATH
or FULL
:
echo Router::build('flower@sakuras', array(), Router::TYPE_RAW);
echo Router::build('flower@sakuras', array(), Router::TYPE_PATH);
echo Router::build('flower@sakuras', array(), Router::TYPE_FULL);
Result:
flower/sakuras
/sites/windwalker/flower/sakuras
http://domain.com/sites/windwalker/flower/sakuras
The RAW route used to store in database, the PATH route used to print in HTML, the FULL route used to redirect.
#Build Route in Controller
If you are in controller, we can use PackageRouter to build route, this way is more safer because we can auto get current package routes.
// Original way, we have to know package name
use Windwalker\Core\Router;
$route = Router::buildHttp('flower@sakura');
// Simpler way to let package handle it
$route = $this->package->router->http('sakura');
$this->setRedirect($route);
#Build Route in View Template
View has also includes package router, just build route like this:
<a href="<?php echo $data->router->html('sakura'); ?>">Link</a>
In Blade
<a href="{{ $router->html('sakura') }}">Link</a>
Twig
<a href="{{ router.html('sakura') }}">Link</a>
If you found a typo or error, please help us improve this document.