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?
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.
I add a bunch of checks on my views such as:
if(userIsGuest)
// then hide info.
Another solution.
What makes the most logical sense in terms of software engineering? Please try to quantify your answer instead of using opinion.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With