How can I return PHP pages?
- ttrepper
- Member | 11
Hi all,
I read through the documentation but unfortunately I do not seem to understand it. Please forgive me if I have missed something. I have a huge ‘old’ webapplication with a lot of php files mixing presentation and logic. While transferring them into a more modern approach using Nette I would like to start wit a simple router catching everything in one method and inside having some ‘intermediate’ logic switching and returning existing ‘old’ php sites. The goal is to subsequently extend the router and strip down the old php files. Is something like that doable?
I have tried something like this to catch all:
//$router = new Nette\Application\Routers\RouteList;
//$router->addRoute('[<path .+>]', 'Front:Bridge:default');
//$configurator->addServices(['router' => $router]);
but I do not understand what I have to do now? I guess it needs a class (?) called “Bridge” with a method called “default” (?) but what is “Front” saying? I would highly appreciate if someone could point me to the next steps please.
Thank you very much in advance,
Thomas
- SamuelThorn
- Member | 29
Hello. I am not sure if I do understand correctly. But I think I had
similar problem to solve. Old PHP application which i wanted to switch to
Nette, but it was not possible to do it all in a single step. I have left the
old application as it was, installed nette in subdirectory ‘/nette/’ and
then started to forward single pages one by one using
.htaccess
.
RewriteRule old-file-01.php /new-url-01 [R,L]
RewriteRule new-url-01 nette/index.php [L]
RewriteRule old-file-02.php /new-url-02 [R,L]
RewriteRule new-url-02 nette/index.php [L]
and in the Nette Router i was catching the new-urls:
$router->addRoute('new-url-01', 'Presenter:action');
Probably not an excelent solution, but it worked for me.
- mskocik
- Member | 62
ttrepper wrote:
but I do not understand what I have to do now? I guess it needs a class (?) called “Bridge” with a method called “default” (?) but what is “Front” saying? I would highly appreciate if someone could point me to the next steps please.
Router tries to match given url to presenters' action. Based on rules you define for Presenter mapping (documentation) – meaning mapping to namespaced presenter class.
In your example your fallback route will try to call presenter called
Front:Bridge
with action default
. It means it will
look for presenter with following mapping:
default
: action to be called on PresenterBridge
: it will look for presenter called BridgePresenterFront
: given BridgePresenter will be in module Front. And where it will look for it, depends on your presenter mapping as defined in yourcommon.neon
. So if we take an example from documentation I linked above:
# example from docs
application:
mapping:
Front: App\Modules\Front\Presenters\*Presenter
Admin: App\Modules\Admin\Presenters\*Presenter
Api: App\Api\*Presenter
It will look for presenter class
App\Modules\Front\Presenters\BridgePresenter
, because
Front
module resolves to App\Modules\Front\Presenters
namespace.
I would strongly recommend you go through Quick start, where you will create simple app, in which you can understand Nette's fundamentals.
- ttrepper
- Member | 11
Thank you very much @mskocik and @SamuelThorn – your answers helped me a lot. I did the quickstart but I have never found something about any naming conventions. So for example does the Presenter have to be named SomePresenter or is a class called “Some” enough? I can try it out but I am wondering if there are any or more (im- or explicit) naming conventions?
Thank you very much in advance,
Thomas
- mskocik
- Member | 62
ttrepper wrote:
Thank you very much @mskocik and @SamuelThorn – your answers helped me a lot. I did the quickstart but I have never found something about any naming conventions. So for example does the Presenter have to be named SomePresenter or is a class called “Some” enough? I can try it out but I am wondering if there are any or more (im- or explicit) naming conventions?
Thank you very much in advance,
Thomas
It's still related to mapping configuration. Please read carefully documentation about Modules
But to answer your question directly…If you have mapping like this in your
common.neon
:
application:
mapping:
*: App\Modules\*\Presenters\*HandlerClass # just an example to make it clear
# this will be translated to:
<any module> map to: App\Modules\<given module>\Presenters\<Presenter>HandlerClass
And your request should be handled by MyModule:Request:default
(from route definition), it will look for:
<?php
namespace App\Modules\MyModule\Presenters;
class RequestHandlerClass extends \Nette\Application\UI\Presenter
{
// your class
}
So as you can see, your presenters (controllers – call them anyway you
want), don't need to have Presenter
in the class name. It's just
Nette's convention. It's the same how some/most others frameworks use word
Controller. Hope it's clear now.
- ttrepper
- Member | 11
Thank you very very much @mskocik. I skipped the documentation about modules so far because I did not want to use them but I have found the naming conventions here "":https://doc.nette.org/…tion/modules#…. Thank you very much for your hint.
May I quickly and finally ask you one mor thing which I have also not found so far please: Is it possible to return a php page in my presenter? Something like this (pseudocode):
namespace App\Modules\MyModule\Presenters;
class RequestHandlerClass extends \Nette\Application\UI\Presenter {
public function doSomething() {
return myOldPhpPage.php; //return here in the meaning of redirect/include the old php but without a 301/302/307/308 status code?
}
}
Thank you very much in advance,
Thomas
- mystik
- Member | 312
Depends on how your old php page is written. Does it just return page content as your code suggests? Or does it render page content directly?
But if it does something else except creating content it might not be possible because things like session handling or sending headers would conflict with how Nette do these things.
- ttrepper
- Member | 11
Hi @mystik – it does not do anything else. It's a lot of html with
php in between which I have to transfer to latte. But as an intermediate step
I would like to return the php only. How can I do that? Would you mind
providing a method snippet please? Thank you very much. 😃
Thomas
- mystik
- Member | 312
If it is the case then I think something like this should work:
class RequestHandlerClass extends \Nette\Application\UI\Presenter {
public function renderSomething() {
$this->sendResponse(new Responses\CallbackResponse(function ($httpRequest, $httpResponse) {
require __DIR__ . '/path/to/myOldPhpPage.php';
}));
);
}
It creates CallbackResponse
that calls given callback when it is
emmited. Given callback simply requires your old script that outputs content.
This response is then send from render method of given action
something
by call to sendResponse()
.
Last edited by mystik (2023-08-11 18:57)