Calculator: PHP and Symfony: Symfony Xampp PHP Root Folder To The Path Environment Variable
Calculator: PHP and Symfony: Symfony Xampp PHP Root Folder To The Path Environment Variable
This document defines a complete walkthrough of creating a Calculator application with the Symfony Framework
Make sure you have installed XAMPP, and added PHP root folder to the path environment variable.
Standard templates (views) reside in the application folder (app) and are usually separated in folder named after
the controller names.
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 1 of 14
The parameters.yml.dist file is very important to contain the same keys as in parameters.yml, because
installing new bundle will delete unused pairs.
Note: extract the folder into a short path (e.g. D:\Projects\Calculator), otherwise you might face
random errors due to the Windows operating system having a path length limit.
Locate the skeleton folder that we gave to you and select the “Calculator” folder from the extracted folder
(e.g. D:\Projects\Calculator):
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 2 of 14
After you click “OK” the project should start loading and indexing. After a few seconds/minutes depending
on your pc, you will be able to work with the project.
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 3 of 14
Open the add menu (+) and add a PHP Script run configuration:
Check the “Single instance only” checkbox. After that, point the File textbox to the bin/console file, which is
inside your project directory:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 4 of 14
After that, add the “server:run” argument to the arguments. In the end, make sure your run
configuration looks something like this:
Now, if you attempt to run it by using the play button on the top right, if everything works correctly, you
should be greeted by this screen on the bottom:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 5 of 14
All the run configuration does is simply run the command line colored in blue, so you don’t have to type it
into a command prompt every time. If you visit localhost:8000 in your web browser, you will be greeted
by the calculator application!
It looks great, but it doesn’t work. So let’s go in and write some code to make those textboxes interact.
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 6 of 14
We have an empty PHP file now. Let’s fill it with stuff. Add the namespace, so Symfony knows it’s an entity.
After that, make a class inside, called Calculator:
2. Create Fields
Now it’s time to start adding fields, which describe our entity, and properties, so it can be accessed from
the outside world. Our calculator will have three fields:
leftOperand the left operand of the calculation. It will have a float data type.
rightOperand the right operand of the calculation. It’ll have the same type as leftOperand.
operator the operator of the calculation (+, -, * or /). It will have a string type
Let’s add the leftOperand field:
The comment above it is actually a PHP annotation. Make sure to add it, otherwise the application won’t
work correctly.
Let’s add the other two fields as well – the right operand and the operator:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 7 of 14
Note that the $operator field has a string type instead of a float type. This is because we’re using it to
store the operator as a string (e.g. "+", "-", "*" or "/").
All this getter method does is return the field from the class. It doesn’t contain any validation per se, but we
could add it in the future by just inserting logic into the method before returning the field.
Let’s create the setter method for leftOperand too:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 8 of 14
The setter takes one $operand as a parameter, sets the leftOperand field to the parameter’s value and
returns the object itself. This will be the blueprint for all classes we make from now on (with different fields
and mutators/accessors, of course). We can also further extend the class by adding functions, which operate
on the class and so on. But more on that later…
For now, let’s just create the rest of the getter and setter methods – for the rightOperand and operator
fields:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 9 of 14
We are done with our Calculator entity, so let’s move on to implementing the action, which makes the app
work.
Looks pretty empty at the moment. All we have is one function, called index, which returns the index view.
No calculation going on here. Let’s make it work like a calculator!
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 10 of 14
The http response we’re giving the client works in the following way: When the client gets a response, the
web browser’s rendering engine renders it onto their screen, turning the HTML we gave the client to a full
webpage.
We’re going to edit the index function to make use of the form, which gets sent by the client and have it
use the values the user sent us through the POST request by clicking the Submit button. In order to do that,
we must first acknowledge the form is being sent at all:
Let’s start by creating a calculator variable where we’ll store our operands and operator:
We’re not quite done yet. We have a bit more work to do. Next, we need to create a form variable, which
will create a special token for the user and also, more importantly, take the values from the form the user
sent us, and stick them in the $calculator variable, so we can work with them:
Next, before we implement the logic for checking if what the user sent us was valid, we have to actually
process the request. We do this with the $form->handleRequest() method:
After that, we can be sure that if we got in-between those two parentheses, the user sent us something we
can actually work with, and the only thing, which remains is to implement the actual calculator logic.
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 11 of 14
2. Implement Calculator logic
If our form was submitted and valid, Symfony automatically inserts the form values into our $calculator
variable. So we can now use that to implement the calculator logic. Let’s start by extracting our operands
and operator into variables for easier typing:
Next, we need somewhere to store the result, right? Right. So let’s make a variable for that:
After that, let’s implement some calculator logic by using a switch case on the operator:
Almost there now… After implementing the logic, shouldn’t that logic yield some result from our little web
app? The answer is yes. After a valid calculation – if everything went well, we should return a HTTP response
to the user with the calculated value:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 12 of 14
Let’s break down this return statement:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 13 of 14
It has some extra logic inside each <option> tag, in the form of twig syntax, which will select the last used
operator when transitioning between calculations. In order to add an operator, we can just copy one of the
<option> tags and edit it to suit our needs:
After which, we can go back to the Calculator Controller and extend the logic to suit our needs:
© Software University Foundation (softuni.org). This work is licensed under the CC-BY-NC-SA license.
Follow us: Page 14 of 14