URL

URL manipulation across the project was never easier.

Printing URLs

All URLs will be printed as absolute URL starting with protocol. That process will first take site_url from configs/application.php and then will append controller, action and any additional parameters according to route rules. Which route will be used is defined in configs/application.php under routing_class key. By default, the Koldy\Application\Route\DefaultRoute is used, which should cover the most of your needs. If you need something extra, please take a look at routes docs.

The href() method

To generate URL for links, form submits or any other GET or POST request, use href method. Href method accepts three parameters and all three are optional. Definition of href method is:


Url::href(string $controllerUrl nullstring $actionUrl null, array $params null);

Make link to home page


Url::home();

Make link to contact page that goes to ContactController


Url::href('contact');

Make link to controller and action

When submitting your form via ajax, you'll probably want to call ContactController and submitAjax method, so for that, use:


Url::href('contact''submit');

Passing additional parameters

If you need to pass additional parameters, pass them on third place:


Url::href('user''profile', array('vkoudela''info'));
Url::href('user''profile', array('id' => 5855'sub_id' => 'second'));
Url::href('user''profile', array('id' => 5855'vkoudela''sub_id' => 2'info'));

The URL format depends on route you're using. If you're using default route, then first example will output this:
• http://your-domain.com/user/profile/vkoudela/info

Second example will return this:
• http://your-domain.com/user/profile?id=5855&sub_id=second

Third example will return this:
• http://your-domain.com/user/profile/vkoudela/info?id=5855&sub_id=2

  • URL format depends on route class. If you need, you can create your own class with completely different rules.
  • More details and examples can be found in Default Route docs.
Do not use href method to generate path to resources or any other static files!

Links to assets

since 1.2.0

This section is about generating URLs to static asset files. If you're running small site, then you probably won't need this, but if you're running high volume site, then you'll need to think about CDN systems or servers that will serve only assets. Koldy supports all of those cases.

Configuration for this is stored in configs/application.php under servers key. By default, this key is empty array and in this case, if you call Url::link('/assets/file.jpg'), it'll return http://your-domain.com/assets/file.jpg.

Let's say you're using some 3rd party CDN service. Then your configuration will be:


'servers' => array(
    
'cdn' => 'http://cdn.example.com'
)

This configuration can be called with:
Url::link('/assets/file.jpg', 'cdn')
Url::cdn('/assets/file.jpg')

Both examples will return http://cdn.example.com/assets/file.jpg.

Note that if you define array('static' => 'http://static.mydomain.com') that you won't be able to call Url::static() because this is PHP's reserved word. In this case, you'll have to use Url::link('/asset/path', 'static').

You can even store your assets in subfolder and configure it:


'servers' => array(
    
'asset' => '/assets'
)

Calling Url::asset('/file.jpg') with this configuration will return http://your-domain.com/assets/file.jpg

Inspecting current URL

Current URL


Url::current();

This method will return the complete current URL, with protocol, domain and request URI.

Current controller


Url::controller();

This method will return string from URL from which controller is detected. Using default route and running http://your-site.com/contact/author, this method will return contact.

Current action


Url::action();

This method will return string from URL from which action is detected. Using default route and running http://your-site.com/contact/author, this method will return contact.

Comparing

You're also able to compare current URL with somehing like this:


Url::current() == Url::href('user''profile', array('vkoudela'))

You can also use several "is" methods to get what you need, such as:


Url::isController('contact');
Url::isAction('submit');
Url::is('contact''submit');

Getting additional parameters

To get additional parameters, use getVar method. If using default routes and you're on http://your-domain.com/user/profile/admin, to get the 3rd segment of URL, you'll need to call this:


echo Url::getVar(2); // prints "admin"

If using default routes and you're on http://your-domain.com/user/profile?id=4324, to get the "id" (the GET parameter), use Url or Input class.


Url::getVar('id');
Input::get('id')
MailParsers