Translate given url to Nette\Application\Request

oldrich.valek
Member | 21
+
0
-

What I need is the opposite of link creation. I have 1 or more url addresses that points at some place on my nette web. I want to translate each of these urls to Nette\Application\Request or just to “model:presenter:action” and parameters.

Is this the proper way? I have searched through the documentation and the api, but I haven't found anything better.

<?php
abstract class BasePresenter extends Nette\Application\UI\Presenter
{
    /** @var \Nette\Http\Request @inject */
    public $httpRequest;

    /** @var \Nette\Application\IRouter @inject */
    public $router;

    /**
     * @param string|Nette\Http\Url $url
     * @return \Nette\Application\Request
     */
    public function discoverAppRequestFromUrl($url)
    {
            $urlScript = new \Nette\Http\UrlScript((string)$url);
            $urlScript->setScriptPath($this->httpRequest->url->basePath);
            $request = new \Nette\Http\Request($urlScript);
            $netteAppRequest = $this->router->match($request);
            return $netteAppRequest;
    }
}
?>

EDIT: Example scenario: BFU edited an article and he enclosed some links pointing at other parts of the website. I would like to store these addresses in a persistent form, so they stay valid even if the domain or routes change.

Last edited by oldrich.valek (2015-08-29 02:29)

Honza Kuchař
Backer | 1648
+
+2
-

IRouter::match() is what you are searching for. https://github.com/…/IRouter.php#L28

But pay attention then. You do need to NOT rename presenters/actions. Otherwise you will be not able to generate links from saved data (Presenter:action) anymore.

If you have old website URL, you can create ONE_WAY route and user getts redirected to the new URL.

So I do not recommend this approach.

old.gandalf
Member | 17
+
0
-

Hello,

what is your point of doing this? If you specify a bit more what you need this for, we could give you better advice.