Changes
Title
unchanged
Yii v2 snippet guide
Category
unchanged
Tutorials
Yii version
unchanged
2.0
Tags
unchanged
tutorial,beginner,yii2
Content
changed
[...]
* [Yii v1 for beginners 2](https://www.yiiframework.com/wiki/462/yii-for-beginners-2)
* [Yii v2 snippet guide I](https://www.yiiframework.com/wiki/2552/yii-v2-snippet-guide)
* [Yii v2 snippet guide II](https://www.yiiframework.com/wiki/2558/yii-v2-snippet-guide-ii)
* [Yii v2 snippet guide III](https://www.yiiframework.com/wiki/2567/yii-v2-snippet-guide-iii)
* [Začínáme s PHP frameworkem Yii2 (I) česky - YouTube](https://youtu.be/ub06hNoL8B8)
**Intro**
---
Hi all!
This snippet guide works with the basic Yii demo application and enhances it. It continues in my series of simple Yii tutorials. Previous two contain basic info about MVC concept, exporting to Excel and other topics so read them as well, but they are meant for Yii v1.
... and today I am beginning with Yii 2 so I will also gather my snippets and publish them here so we all can quickly setup the yii-basic-demo just by copying and pasting. This is my goal - to show how-to without long descriptions.
If you find any problems in my snippets, let me know, please.* [Yii2 App Basic Plus](https://gitlab.com/radin.cerny/yii2-app-basic-plus) ... a compilation of the most used functionalities
* [VueJs3 PWA demo plus](https://gitlab.com/radin.cerny/vuejs3-pwa-demo-plus) ... a basic installable PWA project (written in JavaScript, VueJS3) for my REST offline clients (only 1 data transfer per day)
**Prerequisities**[...]
```
.
.
**Tests - unit + opa**
---
It is easy to run tests as both demo-applications are ready. Use command line and navigate to your project. Then type:
```php
php ./vendor/bin/codecept run
```
This will run Unit and Functional tests. They are defined in folder tests/unit and tests/functional. Functional tests run in a hidden browser and do not work with JavaScript I think. In order to test complex JavaScript, you need Acceptance Tests. How to run them is to be found in file README.md in both demo applications. If you want to run these tests in your standard Chrome or Firefox browser, you will need JDK and file selenium-server*.jar. See links in README.md. Once you have the JAR file, place is to your project and call:
```php
java -jar selenium-server-4.0.0.jar standalone
```
Now you can rerun your tests. Make sure that you have working URL of your project in file acceptance.suite.yml, section WebDriver. For example http://localhost/yii-basic/web. It depends on your environment. Also specify browser. For me works well setting "browser: chrome"... see next chapters ...
**Adding a google-like calendar**[...]
?></li>
<li><?php
$csvUrl = \yii\helpers\Url::current(['exportToCsv' => 1]);
echo Html::a('Preserve filters and sorting', $csvUrl, ['target' => '_blank', 'class' => 'text-left', 'data-pjax'=>'0']);
// 'data-pjax'=>'0' is necessaary to avoid PJAX.
// Now we need to open the link in a new tab, not to resolve it as an ajax request.
?></li>
</ul>
</div>
</div>
<php
// Here goes the rest ...
// echo GridView::widget([
// ...
?>
```
In my code above there were used 2 methods in the model which export things to the CSV format. My implementatino is here:
```php
public static function getCsvHeader() {
$result = [];
$result[] = "ID";
$result[] = "Username";
$result[] = "Email";
// ...
return implode(";", $result);
}
public function getCsvRow() {
$result = [];
$result[] = $this->id;
$result[] = $this->username;
$result[] = $this->email;
// ...
return implode(";", $result);
}
```
**Next chapters had to be moved to a new article!**
---