Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I create guest account pages on my website?

Tags:

html

php

yii

In my website I want to allow users to view some of the features by using a guest account. The guest account would see much of the same information as a regular user but some things will be limited.

What would be a good approach, programming wise, to this problem?

  1. I copy paste the exact pages that a user would normally have to different .php files, example: guestProfile.php. Then I manually remove the buttons or information a guest would see.

  2. I add a bunch of checks on my views such as:

    if(userIsGuest) 
      // then hide info.
    
  3. Another solution.

What makes the most logical sense in terms of software engineering? Please try to quantify your answer instead of using opinion.

like image 549
J_Strauton Avatar asked Oct 24 '25 02:10

J_Strauton


1 Answers

Abstraction is your friend. Rather than stuffing your views with conditionals, you should pass your views any sub-views you wish to be included in the output. Your view doesn't need to know the inner workings of these sub-views, and you can swap them out depending on whether the user is a normal user or a guest user.

Here's a simplistic example:

class View {
    /** Does some view stuff... */

    public function addSubview($name, Subview $subview) {
        //store subviews to be used in the template
    }
}

/** 
 * Somewhere in a controller far far away...
 */
class GuestController extends Controller {
    /** Does some controllery stuff... */

    public function exec() {
        //..
        //prepare the view
        $this->view->addSubview('toolbar', $guestToolbarSubview);
    }
}
like image 162
PiranhaGeorge Avatar answered Oct 25 '25 15:10

PiranhaGeorge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!