M7 Flashcards

1
Q

What is MVC and how it works?

A

Model View Controller - An architectural design pattern.

An HTTP request comes in for the homepage. The URL goes through something called routing that chooses a controller. The controller decides which model and view is needed, combines them, and returns the result to the user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the Model in MVC?

A

The data layer - a class that actually interacts with the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the View in MVC?

A

Output. - What the user actually sees.

The HTML/JSON/XML that is sent back.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the Controller in MVC?

A

The thing that sticks a model and a view together

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why MVC?

A

Seperation of concerns - Any individual layer can be swapped out at any time for a different implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a PHP framework? Give a couple of examples.

A

A framework enforces an architectural pattern.

Laravel is the most popular PHP framework by far.
Symfony is one of the oldest. Actually, it’s not really a framework, but lots of libraries.
Drupal is a cross between a CMS and framework.
Slim is a minimalist framework.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why use PHP framework?

A

Consistency

It’s designed and tested by other software developers, so you know it’s a solid foundation.

Transferable skills

A framework makes it easier to implement MVC, as doing MVC without a framework is actually quite hard. The framework does a lot of it for us.

Routing

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are small/micro frameworks?

A

Small number of files.

Lightweight - doesn’t require huge amounts of memory and/or processing power.

Doesn’t do much for you - does not provide facilities such as authentication and object-relational mapping.

If what you’re building is small now, but hopefully bigger in future, then an enterprise framework would be best.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are large/enterprise frameworks?

A

Large number of files.

Heavyweight - needs powerful servers.

Does lots for you. Contains lots of ‘magic’.

if you aren’t using all the features then you have lots of unneeded files and processing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an oppinionated framework? Pros and cons?

A

A framework, by definition, makes some assumptions about architecture, best practices, and conventions.

An opinionated framework will only give you one way to do things.

Opinionated framework:
Pro - Forced consistency
Con - Less flexibility

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is routing and its benefits?

A

Routing allows you to map a URL to a particular piece of code, but there doesn’t need to be any relationship between the URL and the file(s) being used.

Benefits:
Your file structure does not have to match the URL structure.

URLs can be dynamic.

A bit of added security. Not much, but it does hide the file structure to a degree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What folder structure does Slim framework have?

A

public - Static content like CSS and JS.
app - The application code required to run Slim.
src - Our custom application code.
templates - .phtml files, also known as views.
tests - Unit tests.
vendor - Third party stuff like phpunit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does routing work in Slim ?

A

Slim 4 provides a routing system that takes a web request and returns a response depending on the application logic in a request handler.

$app->get(‘/’, callableClass)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Templating Engine?

A

A template engine enables you to use static template files in your application.

At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Dependency Injection?

A

Dependency injection is a design pattern.

It is passing a dependency to an object rather than instantiating it inside the object.

Can be done by passing the dependency as a parameter to a constructor or a setter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is loose coupling?

A

Loose coupling makes the application more flexible, so individual parts can be replaced without having to throw the whole thing out.

17
Q

What are the pros/cons of dependency injection?

A

Dependency injection pros:
It makes parts of the application more loosely coupled.
It makes the application more maintainable.
It makes code more testable.

Dependency injection cons:
A bit more code to write.

18
Q

What is the factory design pattern?

A

A factory is a function or class method that knows how to instantiate an object with all of its dependencies.

19
Q

Benefits of the factory design pattern?

A

Benefits
DRY-er: you don’t have to keep writing the code to instantiate an object each time you want one.
Keeps things consistent: there is only one correct way to instantiate the object.
You no longer need to know or care about the dependencies: the factory takes care of it - encapsulation.

20
Q

What is a Dependency Injection Container (DIC) ?

A

Reduces code smell of too many constructor parameters.

A centralised object or array, where you store all of your factories.

Groups all your factories into one place - Single source of truth

Whenever you need an object, you can ask the DIC and it will call the factory for you.

21
Q

What are viewhelpers?

A

ViewHelpers separate or abstract out logic that is only used for displaying data.

22
Q

What is the checklist for the view in Slim?

A

.phtml file in templates.
.php view helper in src/ViewHelpers. (A view helper is optional.)
Route to callable controller in routes.php.
Controller in src/Controllers.
Callable factory to make controller in src/Factories.
Factory registered with DIC in app/dependencies.php under key of controller name.

23
Q

What is the checklist for the Model in Slim?

A

.php in src/Models.
Callable factory to make model in src/Factories.
Factory registered with DIC in app/dependencies.php under key of model name.

24
Q

What is an API?

A

Application programming interface. A way of one code base talking to another.

25
Q

What is REST ?

A

A set of rules/principles for creating an API.

RE - Representational
S - State
T - Transfer

A way of standardising APIs.

REST is industry best practice to make development easier/more consistent and increase client adoption.

26
Q

What are the REST rules?

A

Stateless.
Usually authenticated.
Calls via HTTP verbs (PUT, GET, POST, DELETE).
A structured response in a consistent format - possibly the most important point.

27
Q

Describe the REST rule - stateless?

A

Does not hold/store any information about the client.(browser/postman)
One call does not affect another.
Doesn’t store anything about the calls that have been made.

If you write an API and use sessions, it is not stateless! Hence it is not a REST API.

28
Q

Describe the REST rule - authenticated?

A

The stateless requirement causes a problem for authentication:
How do you create logins without a session?

Use cookies, sent in every HTTP request.

A login token is included with each request that allows access to the API.

29
Q

Describe the REST rule - calls Via HTTP Verbs?

A

A truly RESTful API should handle all of the HTTP verbs for each endpoint, even if they just give a failure response.

PUT - Changes or updates data.

POST - Creating new data.

GET - Gets data (does not make changes).

DELETE - Delete data or mark data as deleted.

If the API is required to send an email or SMS, or manipulate a database, then it should do so in response to PUT, POST or DELETE.

30
Q

Describe the REST rule - consistency?

A

Consistency
The data format is always the same, e.g. JSON, XML, or plain text.

A standard response format keeps the application consistent and predictable

31
Q

What do we mean by RESTful?

What are the main things for RESTful?

A

We say “RESTful” because it is not always possible to stick to all the REST ‘rules’.

RESTful means sort of REST. Where a developer chooses to implement some of the REST guidelines but not all. Generally because the rules are too strict or unnecessary for the application.

Main things:
Consistent data
Good end points
Good documentation