PHP code example of vinogradsoft / navigator
1. Go to this page and download the library: Download vinogradsoft/navigator library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vinogradsoft / navigator example snippets
/** relative url */
echo $urlBuilder->build('/user', ['id' => 100]); # /user/100
/** absolute url */
echo $urlBuilder->build('/user', ['id' => 100], true); # http://mydomain.ru/user/100
use Navigator\ArrayRulesProvider;
use Navigator\UrlBuilder;
ArrayRulesProvider([
'user' => '/user[/{var_name}]',
// many other route definitions in key-value format.
])
);
echo $urlBuilder->build('/user', null, true), '<br>'; # https://vinograd.soft/user
echo $urlBuilder->build('/user', ['var_name' => 'var_value'], true), '<br>'; # https://vinograd.soft/user/var_value
echo $urlBuilder->build('user', ['var_name' => 'var_value'], true), '<br>'; # https://vinograd.soft/user/var_value
echo $urlBuilder->build('/user', ['var_name' => 'var_value']), '<br>'; # /user/var_value
echo $urlBuilder->build('user', ['var_name' => 'var_value']), '<br>'; # user/var_value
$urlBuilder = new UrlBuilder(
'https://vinograd.soft',
new ArrayRulesProvider([
'user' => '/user/profile',
])
);
echo $urlBuilder->build('/user', null, true); # https://vinograd.soft/user/profile
new ArrayRulesProvider([
'blog/post/view' => '/post/{id:\d+}',
]);
new ArrayRulesProvider([
// Not working warrant /blog/post/view
'/blog/post/view' => '/post/{id:\d+}',
]);
$urlBuilder = new UrlBuilder(
'https://vinograd.soft',
new ArrayRulesProvider([
'user' => '/user[/{var_name}]',
'about' => '/about[.html]',
])
);
echo $urlBuilder->build('about', ['.html' => true], true); # https://vinograd.soft/about.html
echo $urlBuilder->build('/user', ['var_name' => 'my_unique_value'], true);
# https://vinograd.soft/user/my_unique_value
echo $urlBuilder->build('/user', ['var_name' => 'my_unique_value']); # /user/my_unique_value
echo $urlBuilder->build('user', ['var_name' => 'my_unique_value']); # user/my_unique_value
echo $urlBuilder->build('user', null, true); # https://vinograd.soft/user
$externalAddress = 'http://another.site:8080/path/to/resource';
echo $urlBuilder->buildExternal([
':src' => $externalAddress,
':scheme' => 'ftp',
':user' => 'grigor',
':password' => 'password123',
':port' => '21'
]);
# ftp://grigor:[email protected] :21/path/to/resource
echo $urlBuilder->buildExternal([
':src' => 'http://another.site',
':path' => ['blog', 'post', 41],
]);
# http://another.site/blog/post/41
echo $urlBuilder->buildExternal([
':src' => 'http://another.site',
':scheme' => 'https',
]);
# https://another.site
echo $urlBuilder->buildExternal([
':scheme' => 'https',
':host' => 'another.site',
':path' => ['path', 'to', 'resource'],
]);
# https://another.site/path/to/resource
echo $urlBuilder->buildExternal([
':src' => 'http://another.site',
':user' => 'user'
]);
# http://[email protected]
echo $urlBuilder->buildExternal([
':src' => 'ftp://another.site:21',
':user' => 'grigor',
':password' => 'password123'
]);
# ftp://grigor:[email protected] :21
echo $urlBuilder->buildExternal([
':scheme' => 'http',
':host' => 'another.site'
]);
# http://another.site
echo $urlBuilder->buildExternal([
':scheme' => 'http',
':host' => 'another.site',
':port' => '5000'
]);
# http://another.site:5000
echo $urlBuilder->buildExternal([
':src' => 'https://another.site',
':path' => ['path', 'to']
]);
# https://another.site/path/to
echo $urlBuilder->buildExternal([
':src' => 'https://another.site',
':path' => 'path/to'
]);
# https://another.site/path/to
echo $urlBuilder->buildExternal([
':src' => 'https://another.site',
':path' => 'path/to',
':suffix' => '.html'
]);
# https://another.site/path/to.html
echo $urlBuilder->buildExternal([
':src' => 'https://another.site/path/to?q=value#news',
':suffix' => '-city'
]);
# https://another.site/path/to-city?q=value#news
echo $urlBuilder->buildExternal([':src' => 'https://another.site', '?' => ['s' => 'Hello world']]);
# https://another.site/?s=Hello%20world
echo $urlBuilder->buildExternal([
':src' => 'https://another.site',
'?' => 's=Hello world'
]);
# https://another.site/?s=Hello%20world
[
'search' => [
'blog' => [
'category' => 'news',
'author' => 'Grigor'
]
]
]
echo $urlBuilder->buildExternal([
':src' => 'https://another.site',
'?' => [
'search' => [
'blog' => [
'category' => 'news',
'author' => 'Grigor'
]
]
]
]);
# https://another.site/?search%5Bblog%5D%5Bcategory%5D=news&search%5Bblog%5D%5Bauthor%5D=Grigor
echo $urlBuilder->buildExternal([
':src' => 'https://github.com/vinogradsoft/compass',
'#' => 'quick-start'
]);
# https://github.com/vinogradsoft/compass#quick-start
namespace <your namespace>;
use Compass\DefaultUrlStrategy;
use Compass\Url;
class ReferralUrlStrategy extends DefaultUrlStrategy
{
/**
* @inheritDoc
*/
public function updateQuery(array $items): string
{
$items['refid'] = 222;
return http_build_query($items, '', '&', PHP_QUERY_RFC3986);
}
/**
* @inheritDoc
*/
public function forceUnlockMethod(
bool &$schemeState,
int &$authoritySate,
int &$relativeUrlState,
array $items,
array $pathItems,
array $queryItems,
bool $updateAbsoluteUrl,
?string $suffix = null
): void
{
$relativeUrlState &= ~Url::QUERY_STATE;
}
}
$urlBuilder = new UrlBuilder(
'https://vinograd.soft',
new ArrayRulesProvider([
'user' => '/user[/{var_name}]'
]),
['referral' => new ReferralUrlStrategy()]
);
echo $urlBuilder->buildExternal([
':src' => 'https://another.site/path/to/resource',
':strategy' => 'referral'
]);
# https://another.site/path/to/resource?refid=222
$externalUrl = Url::createBlank();
$externalUrl->setUpdateStrategy(new ReferralUrlStrategy());
$urlBuilder = new UrlBuilder(
'https://vinograd.soft',
new ArrayRulesProvider([
'user' => '/user[/{var_name}]'
]),
[],
null,
$externalUrl
);
echo $urlBuilder->buildExternal([
':src' => 'https://another.site/path/to/resource'
]);
# https://another.site/path/to/resource?refid=222
echo $urlBuilder->buildExternal([':src' => 'https://россия.рф', ':idn' => true]);
# https://xn--h1alffa9f.xn--p1ai
php composer
php composer tests