2

I have an action_register function which attempts to register a new user. If successful, I would like the user to be forwarded to the login form (which is shown via action_login). However, I'd also like to send a $message and $message_type to the login view that says something like "Your account has been created, please log in".

What's the best way to do this. By best I mean maintainable, clean and secure.

My ideas up to now:

  • POST the data to the url routed to action_login and redirect.
  • Find a way to send the data right to the controller method and have action_login render as always.
tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

5

You actually don't need to do any snippet of code to set session Message , Fuel php provides a very good interface to handle such feature . Its the core Session Class here is how we need to use as per documentation .

in your case lets say you need to redirect to login_action from register_action and after successful registration process , then just set the flash message like below

Session::set_flash('success', 'Your account has been successfully created !'); 

Later in the view file of the login you can just use the below code to get the flash message that you want . some what like this

<?php if (Session::get_flash('success')): ?>
    <div class="alert-box info">
        <?php echo  Session::get_flash('success') ; ?>

    </div>
<?php endif; ?>

Also You can delete the session message say 'success' in the above example by just using delete_flash method like below

Session::delete_flash('success');

To know more about session class you can learn from the link below

FuelPhp Session Class docuementation

And also to redirect the user you don't need to write any new PHP code, Fuel php gives a nice interface for that too , its the 'Response' class which has methods defined for you to do that

Here is how you need to redirect the user in any of your controller action

Response::redirect('yourRouteName');

Its always a good practice to exploit the classes defined in the framework , which are actually defined with an intention to make the developers life easy .

Being object oriented in your programming approach especially when using a good quality OOP MVC framework like fuelPhp prevents you from reinventing the wheel always ( DRY ) by providing you access to ready made Getters and setters .

Hope this helps

Community
  • 1
  • 1
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
  • 1
    While @Telvin Nguyen's code might work as intended, using the classes that the framework has to offer is the right way to do things. I vote on this one! – Quetzy Garcia Apr 10 '13 at 21:09
  • Yeah this is what I was doing when I inquired about setting flash variables instead. Works marvellously. –  Apr 10 '13 at 21:23
1

The message in this case just display one time only (after registration), but I believe in future your project will come with many messages like that.

You should make snippet of code to do that thing and for the resusability - After you do something as result, you need to store message into a session variable, then redirect to other action Ex:

    $_SESSION['msg'] = 'Your account has been created, please log in';
  • On the screen destination, you check if that session variable was set, then you print the message that was stored on session var and clear it.

            <?php
            if(isset($_SESSION['msg']){
            ?>
            <script>
            //call some javascript function to display popup for example
            var msg = '<?php echo $_SESSION['msg']; ?>';
            display_popup_message(msg);
    
            function display_popup_message(msg){
                alert(msg);
            }
            </script>
            <?php
            unset($_SESSION['msg']);
            }
            ?>
    
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
  • Ah right session variables, that makes much more sense. Should I maybe set a flash session variable? –  Apr 10 '13 at 15:27
  • FuelPHP doesn't use PHP native sessions, so by default this will not be passed on to the next request. aravind.udayashankara's anwser is the correct way of doing it. – WanWizard Apr 11 '13 at 10:41