Changes
Title
unchanged
Yii3 - How to start
Category
unchanged
Tutorials
Yii version
unchanged
3.0
Tags
unchanged
Content
changed
(draft - all will be retested later)
# Intro
In Yii3 it is not as easy to start as it was with Yii2. You have to install and configure basic things on your own. Yii3 uses the modern approach based on independent packages and dependency injection, but it makes it harder for newcomers. I am here to show
them ...
Note:
- all how I did it.
> All the code is available in my new [GitHub repository](https://github.com/rackycz/yii3api). I will be using it as a boiler-plate for my future projects so it should be always up-to-date and working.
Instead of installing local WAMP- or XAMPP-server I will be using Docker.
- Do not forget about a modern IDE like PhpStorm, which comes bun
dled with all you will ever need.
## PSR Standards by Framework Interoperability Group
First of all, learn what [PHP Standards Recommendations](https://en.wikipedia.org/wiki/PHP_Standard_Recommendation) by [Framework Interoperability Group (FIG)](https://www.php-fig.org/psr) are. It will help you understand why so many "weird" PSR imports are in the Yii3 code. In short: These interfaces help authors of different frameworks to write compatible classes so they can be reused in any other framework following these principles.
## Dependency injection + container
Check [this YouTube video](https://www.youtube.com/watch?v=TqMXzEK0nsA) for explanation
## invoke()
The `__invoke()` public method is called when you call the **instance** as a method. (Therefore the constructor was already executed)
```php
$obj = new MyObj(); // Now the __construct() is executed.
$obj(); // Now the __invoke() is executed (The instance is needed!)
```
I never used it, but prepared a following example that shows when invoking can be applied:
```php
class MyUpper
{
public function __invoke($a) { return $this->go($a); }
public function go($a) { return strtoupper($a); }
}
$instance = new MyUpper();
$array = ['a', 'B', 1, '1'];
// __invoke is used:
var_dump($instance($array[0]));
var_dump(array_map($instance, $array));
// These do the same without invoking:
var_dump(array_map('strtoupper', $array));
var_dump(array_map([$instance, 'go'], $array));
var_dump(array_map(function($a) use ($instance) { return $instance->go($a); }, ['a','B',1,'1']));
```
## Hash annotations for class attributes
PHP 8 introduces annotations like this (not only for class attributes):
- `#[Column(type: 'primary')]`
- `#[Column(type: 'string(255)', nullable: true)]`
- `#[Entity(repository: UserRepository::class)]`
- `#[ManyToMany(target: Role::class, through: UserRole::class)]`
They should replace the original DocBlock annotatinos and provide more new functionalities.
Learn what they mean and how they are used by Yii3. To me this is a brand new topic as well.
# Yii3 - How to start
Yii3 offers more basic applications: Web, Console, API. I will be using the API application:[...]
## Running the demo application
You may be surprised that docker-compose.yml is missing in the root. Instead the "make up" and "make down" commands are prepared.
If you run both basic commands as mentioned in the documentation:[...]
... then the web will be available on URL
- http://localhost:80
- If
you check the returned data you will see a `<xml>` inside the browser. In order to obtain JSON-response, paste the URL into Postman. (so called "content negotiation" does this auto-decision)
If you want to modify the data that was returned by the endpoint, just open the action-class `src/Api/IndexAction.php` and add one more element to the returned array.run via browser, XML is returned
- If run via Postman or Ajax, JSON is returned
If you want to modify the data that was returned by the endpoint, just open the action-class `src/Api/IndexAction.php` and add one more element to the returned array.
> You may be missing 'docker compose stop' or 'make stop', because 'make down' removes your containers and drops your DB. In that case you can add it to the Makefile in the root (see below). If you then type 'make help' you will see the new command.
```sh
ifeq ($(PRIMARY_GOAL),stop)
stop: ## Stop the dev environment
$(DOCKER_COMPOSE_DEV) stop
endif
```
## Adding DB into your project[...]
composer require yiisoft/db-mysql
composer require yiisoft/cache
composer require yiisoft/db-migration --dev
```[...]
```
Next we also need an algorithm that will enforce these tokens in each request, will validate and refresh them and will restrict access only to endpoints that the user can use. This is a bigger topic for later. It may be covered by the package https://github.com/yiisoft/auth/ which offers "HTTP bearer authentication".
## JS client - Installable Vuejs3 PWA
If you create a REST API you may be interested in a JS frontend that will communicate with it using Ajax. Below you can peek into my very simple VueJS3 attempt. It is an installable PWA application that works in offline mode (=1 data transfer per day, not on every mouse click) and is meant for situations when customer does not have wifi everywhere. See my [Gitlab](https://gitlab.com/radin.cerny/vuejs3-pwa-demo-plus).