M7 Flashcards
What is MVC and how it works?
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.
Describe the Model in MVC?
The data layer - a class that actually interacts with the database
Describe the View in MVC?
Output. - What the user actually sees.
The HTML/JSON/XML that is sent back.
What is the Controller in MVC?
The thing that sticks a model and a view together
Why MVC?
Seperation of concerns - Any individual layer can be swapped out at any time for a different implementation.
What is a PHP framework? Give a couple of examples.
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.
Why use PHP framework?
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
What are small/micro frameworks?
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.
What are large/enterprise frameworks?
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.
What is an oppinionated framework? Pros and cons?
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
What is routing and its benefits?
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.
What folder structure does Slim framework have?
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 does routing work in Slim ?
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)
What is a Templating Engine?
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.
What is Dependency Injection?
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.
What is loose coupling?
Loose coupling makes the application more flexible, so individual parts can be replaced without having to throw the whole thing out.
What are the pros/cons of dependency injection?
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.
What is the factory design pattern?
A factory is a function or class method that knows how to instantiate an object with all of its dependencies.
Benefits of the factory design pattern?
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.
What is a Dependency Injection Container (DIC) ?
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.
What are viewhelpers?
ViewHelpers separate or abstract out logic that is only used for displaying data.
What is the checklist for the view in Slim?
.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.
What is the checklist for the Model in Slim?
.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.
What is an API?
Application programming interface. A way of one code base talking to another.