panique / mini

Just an extremely simple naked PHP application, useful for small projects and quick prototypes. Some might call it a micro framework :)
1.35k stars 478 forks source link

Send message to another controller? #229

Open abdhass opened 7 years ago

abdhass commented 7 years ago

Hi

Say i have a user login view (site/login). On submit goes to a method (site/loginUser) and in that controller method I want to send the user back to site/login but with a message (in my case a error message to say they couldnt log in), how would i do that?

I tried making a public property called message in my controller and setting it in loginUser() method and then the login() method will check if its filled and so the view will show message, but this doesnt work as the class will create itself again when sent to location: site/login.

Currently I put everything in one controller method, this checks if form submitted, if error then add to error array and the view will show this. if form not submitted, show the view form.

Any ideas? thanks

bravedave commented 7 years ago

Presumably you get to the site/login with some sort of redirect

so pass the message with the redirect

in straight php that is:

<?php
    header( "location: /site/login\n");

so:

<?php
    header( sprintf( "location: /site/login?msg=%s\n", urlencode('big fail'));

then on your login form:

<p><?php if ( isset($_GET['msg'])) print $_GET['msg'] ?></p>
abdhass commented 7 years ago

thanks, thats one way. I went with IF logic in my method. that way, i can load the view with a error message.

Maybe i understand OOP in PHP wrong.

Seems like every page you hit, the application will restart. So any variables etc saved in-memory will get lost. How do people handle that?

p.s I have learnt a lot from this project and its code. Thank you.

dannysmc95 commented 7 years ago

Hi @abdhass, this is the base of an MVC model, what happens is every request is redirected to index.php and then the MVC framework takes over. So you are right each time you request a page the MVC framework is run each time.

Saving variables can be done with files, cookies, session or a database. Depends on the type of content you need to store.