Checks the value of SESSION variables classified as business logic?

I am developing my web application. I have this piece of code:

<?php if($_SESSION['add'] == 1) echo '<input type="button" name="add" id="add" value="Add" onclick="add()" >'; if($_SESSION['edit'] == 1) echo '<input type="button" name="edit" id="edit" value="Edit" onclick="edit()">'; ?> 

Basically, when a user logs in, I set session variables that indicate whether the user is allowed to modify or add entries. Therefore, when they get to the home page, I use this code to determine whether to show or not add add and edit buttons.

Is this code a violation of the logic / presentation separation rule? If so, how can I achieve separation?

I do not use any web frameworks.

+7
optimization html php
source share
1 answer

While the conditions in the view are perfectly visible, your View should not try to extract any data from any source on its own. This is not his role, and it should only work with the data that your Controller (or Presenter, depending on which application architecture you have) feeds. Your view should not know the logic of why and when to switch between edit and add modes. He must know how to do this when ordering. In your case, the Controller should check $_SESSION and decide which mode to add or change your view should display and pass this decision to your view (i.e. action_mode = edit|add ) for silent execution.

PS: I recommend always putting code blocks (even single-line ones) in brackets { , } .

+10
source share

All Articles