steampixel / simplePHPRouter

This is a simple and small single class PHP router that can handel the whole url routing for your project.
MIT License
406 stars 117 forks source link

How to use Views with this Router? #59

Closed yFlixxx closed 2 years ago

yFlixxx commented 2 years ago

Hello, I really like this Router so I used it in my project. Now I have one question:

Router::add('/login', function() {
    View::make('Login');
}, ['get','post']);

View is just a function to require a specific php file. But how can I use the $_POST request in this file? For example In the Login.php which is used in the /login Route, I want to process the Input made in the form.

How can I achieve this? Greetings

ibnsultan commented 2 years ago

the router does not provide such functionality but you can extend it by using an external templating engine like blade, refer to this repository for more info blade template

steampixel commented 2 years ago

I would recommend pushing your data to your view. I have made a little PHP-only templating lib (maybe similar to your View class) named SimplePHPComponents. Just look at this example code:

Route::add('/user/([0-9]*)/edit', function($id) {
  Component::create('page/edit-user')->assign(['id' => $id, 'some_other_data' => $_POST['some_other_data']])->print();
}, 'get');

Or just look at https://github.com/steampixel/simplePHPPages In this boilerplate I combined this router with simplePHPComponents

yFlixxx commented 2 years ago

I tried this but it doesn't seem to work properly. Used this as my Route.

Router::add('/login', function() {
    $user = $_POST['username'];
    View::make('Login', array($user));
}, ['get','post']);

And modified my View func to this:

public static function make($view, array $args = array()) {
           extract($args);
           require_once( 'views/'.$view.'.php' );
}

But on the Login.php file $args[0] doesn't return any value. Also this workaround doesn't seem very secure.

steampixel commented 2 years ago

Please debug your route and see whats inside your $_POST

Router::add('/login', function() {
   print_r($_POST);
   exit();
   // $user = $_POST['username'];
   // View::make('Login', array($user));
}, ['get','post']);

Also the extract function needs an associative array! Try using View::make('Login', array('user' => $user)); instead.

steampixel commented 2 years ago

This approach is secure as long as you sanitize your POST data properly.

yFlixxx commented 2 years ago

Ok, I figured out the issue. I had a Input class that checks if any POST Data is send, which doesn't work in this system. So is there any way to check if any data is send with the View function so I can modify the Input class? Thanks in advance for the help!

ghost commented 2 years ago

your route file should remain a route file only. your input class should be called within your 'view' class or within the function() {}. that way $_POST is already defined and you can now test if $_POST['username'] exist and is valid or not.

Ideally, you should use a constructor before using your view::make() to cover the logic.

Good luck!