Using Route_Action server event (v2021)

Tips submitted by PHPMaker users
Post Reply
arbei
User
Posts: 7961

Using Route_Action server event (v2021)

Post by arbei »

In pre-v2021 versions, the generated application was file based, if you wanted to add a custom page, you added a Custom File so you can access, e.g. /mypage.php. In v2021, routing is used, if you want a page with header and footer, you can still use Custom File with Include common files enabled so that PHPMaker will generate a route, e.g. /mypage. However, if you just want to do an action on the server side and return the result without header and footer, now there is an alternative way (and simpler way, depending on your usage), you can use the new Route_Action server event to add routes with or without parameters, e.g.

Code: Select all

$app->get('/books/{id}', function ($request, $response, array $args) {
$name = ExecuteScalar("SELECT name FROM books WHERE id = " . AdjustSql($args['id'])); // Get book info by $args['id'], here assume id is integer
$response->getBody()->write($name); // Write to response body
return $response; // Return the response
});

See How to create routes for details.


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

Thank you for pointing me in right direction!

Did some testing and that works great!

But when I need to generate a Word doc with PHPOffice and uses the keyword USE inside the function I get problems (Should be in the beginning of file). How, with PHPmaker and routes, do I handle that?


arbei
User
Posts: 7961

Post by arbei »

You may use fully qualified name, see Using namespaces: Basics.


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

Thanks again for teaching me the basics!

If anybody else with my lack of knowledge in namespaces; this is what I did:

Instead of using the following:

Code: Select all

use PhpOffice\PhpWord\Style\Paper;
use PhpOffice\PhpWord\Style\Language;
use PhpOffice\PhpWord\Writer\Word2007\Style\Font;

I have to write the full qualified name when using the functions in PhpOffice like this:

Code: Select all

$phpWord = new \PhpOffice\PhpWord\PhpWord()
$paper = new \PhpOffice\PhpWord\Style\Paper();

Also the constants f.ex language needs the full qualified name:

Code: Select all

\PhpOffice\PhpWord\Style\Language::SV_SE

So moving from separate php-file into routes when creating phpword documents I did this:

Server Events->Global->All pages -> Route_Actions

Code: Select all

$app->get('/createdoc/{id}', function ($request, $response, array $args) {
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $paper = new \PhpOffice\PhpWord\Style\Paper();
        $phpWord->getSettings()->setThemeFontLang(new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::SV_SE));

Do all stuff about the same and end with this:

Code: Select all

        $response->getBody()->write(''); // Write an empty response body so you don't interfere with the creation of the Word document.
        return $response; // Return the response 
    });

At least this worked for me, perhaps it can be made more neat but it works!


Post Reply