📜 ⬆️ ⬇️

Yii2, quick start. The easiest site on Yii2 with static pages without using a database

First, I wrote a lengthy preface to this text, and then deleted it. In general, this text is for those who, like me, love to do first, and then to understand what he did. To collect the information below, I had to rummage around for various days in various sources. So, I hope someone will come in handy. This is a good cheat sheet.

1. Install Yii2 Basic on the server, as written in the documentation . Everything is described there clearly and in the case, I have nothing to add.

Unless here: locally on OpenServer it was put without a hitch, without a hitch. But remotely, on KVM Ubuntu 16.04 with 1Gb of RAM, Composer did not have enough memory. Fought two hours, and did not master. Fortunately, the site can be easily transferred from a local server to a remote one by simple copying.
')
2. I repeat, the installation is basic (for example, to the / www / site / basic folder, but then, for simplicity, we will count from the basic folder in which you created the project, as recommended in the manual).

You immediately install the basic / web folder as a DocumentRoot - how to do this is also in the official Russian manual .

3. Open basic / controllers / SiteController.php and change

public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } 

on

 public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], 'page' => [ 'class' => 'yii\web\ViewAction', ], ]; } 


This will allow you not to write a separate action for each static page. From now on, they will be picked up by you automatically from the folder basic / views / site / pages.

Create a one.php file in this folder and copy the contents of the basic / views / site / about.php file into it (for clarity).

Now the contents of this file are available at:

http://yoursite.com/index.php?r=site%2Fpage&view=one

Accordingly, you can create as many files as you need.

4. Turn on the CNC as written here .

Settings are made in the file basic / config / web.php.

5. To url

http://yoursite.com/index.php?r=site%2Fpage&view=one

looked like

http://yoursite.com/one

you need to set such a rule for CNC

'view' => 'site / page',

(Attention !!! The word view in the above example should be taken in angle brackets, could not write as it is, because Habra-HTML-filters do not pass, but below - everything is fine, paradox ... * shrug *)

That is, the piece of code responsible for the CNC in basic / config / web.php will look like this:

 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'enableStrictParsing' => false, 'rules' => [ '<view>' => 'site/page', ], ], 

6. Now the meta tags for each page. Initially, only the title is set there.

 $this->title = '  one.php'; 

And we, for example, need to add OG-meta tags (if you don’t know what it is - by all means google it - an indispensable thing for social networks). Write straight below line something like:

 $this->registerMetaTag([ 'property' => 'og:title', 'content' => 'OG-  one.php' ]); 

The rest of the meta tags are added by analogy with this. If something is unclear - there is documentation on registerMetaTag ()

7. Trick for og: url

Under the line

 use yii\helpers\Html; 

at the very top of the page, add:

 use yii\helpers\Url; 

And og: url you register so:

 $this->registerMetaTag([ 'property' => 'og:url', 'content' => Url::to('',true) ]); 

Now, with any transfers from server to server, the og-url will be fine.

8. The same with internal links on the site. Connect the helper (at the top of the page):

 use yii\helpers\Url; 

And, for example, a link to our one-page is placed in this form:

 <?= Url::to(['site/page', 'view' => 'one']); ?> 

It will now display correctly no matter what rules you set for the CNC.

And yes, please note that in the NavBar layout widget ( basic / views / layouts / main.php ) the same link will look something like this:

 ['label' => '  One', 'url' => ['/site/page', 'view' => 'one']], 

That's all. A simple site on Yii2 is quite ready to work (it’s a pity that smiles are forbidden to be installed).

Anyway, you still need to read the Yii2 documentation, at least, the introductory part. But now you know how to immediately create a ready-to-use web application, and not just a blank. If I had such a guide right away - then I would not have been sitting online for three days, but I would have walked more. Spring is the same.

Be healthy!

Source: https://habr.com/ru/post/323416/


All Articles