Monday, March 9, 2015

Activate friendly url in yii2

What is friendly url?
friendly URL is a Web address that is easy to read and includes words that describe the content of the webpage. Friendly url can help visitors remember the web address and it is also a factor for search engine optimization.

How to activate it in yii2
Friendly url in yii2 is not activated by default. To activate it we need edit our configurations at
"config/web.php"

This is the content of web.php
$params = require(__DIR__ . '/params.php');
$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'MRPGvrWJP19gG6ABE8zAzpOvg_I6Lm1J',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;
inside 'components' => [] we will add a code to activate friendly url
'urlManager' => [
   'enablePrettyUrl' => true,
   'rules' => [
    // your rules go here
   ],   
  ],
The result must look like this
$params = require(__DIR__ . '/params.php');

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
       'urlManager' => [
           'enablePrettyUrl' => true,
           'rules' => [
               // your rules go here
            ],   
       ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'MRPGvrWJP19gG6ABE8zAzpOvg_I6Lm1J',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => require(__DIR__ . '/db.php'),
    ],
    'params' => $params,
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'debug';
    $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';
}

return $config;

Remove index.php in Url
Following the instruction above, Our url link will look like this http://localhost/web/index.php/site/about
If you want to remove index.php in the url, We need to add  'showScriptName' => false, settings in our url manager.
The result of our url manager settings must look like this
'urlManager' => [
   'enablePrettyUrl' => true,
   'showScriptName' => false,       
   'rules' => [  
            
   ],   
  ],
Remember that this will lead to error since we remove index.php in our url. In order to resolve this, We need to add a new .htaccess file in our /web/ folder,
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Make root folder accessible and redirect to web folder in yii2

In yii2, by default, the root folder will show the list of file under it when viewed in browser.

To view the actual website, We need to go inside the /web/ folder
ex. http://www.example.com/basic/web/index.php

in order for us to go to http://www.example.com/index.php and display the content of our /web/ folder
We need to edit our .htaccess file
Options +FollowSymLinks

RewriteEngine On
RedirectMatch ^/$ /web/

note that Options +FollowSymLinks is an option to activate the rewriteengine on my Uniserver, In other server, This may not be required

How to make rewrite engine work on Uniserver

Inorder for the "RewriteEngine On" to work in Unizeserver we just need to put
Options +FollowSymLinks
inside our .htaccess file

Saturday, March 7, 2015

Intalling yii2 in windows using composer

In this tutorial, I will show you how to get started in yii2. First you need to choose which template to use. For this tutorial we will use Basic Application Template.

Before we can install via composer, we need to install composer in our windows computer.
A detailed tutorial can be found here
How to install composer in windows with php.exe
After Installing Composer, we can now start to use it by opening our  command terminal




















We need to change directory first using "cd" going to our uniservers path




 At this point, We are inside www folder, We will tell composer to install yii files here.

composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic namesample
 

 

 This may take a while, It will install all yii2 files inside our www folder.

After the installation process is done, We can now check our www folder.

\www\namesample is generated

 Your folder must look like this



















You can visit this page by running your uniserver and pointing it to

http://localhost/namesample/web/




If you can see this page, Then you have successfully created your Yii-powered application.


How to install composer in windows with php.exe

In this tutorial, I will show you how to install composer on windows 7.

So what is Composer? 
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

You can download the windows installer at https://getcomposer.org/download/
















As you notice when installing, It will ask you where your php.exe is located. as a default, Windows7 doesn't have php included. We can solve this by using our uniservers preinstalled php.




















What is Uniserver?
Uniserver or Uniform Server is a lightweight server solution for running a web server under the WindowsOS. It is less than 10MiB, it includes the latest versions of Apache2, Perl5, PHP5, MySQL5, phpMyAdmin and more.

You can download UniserverZ at http://www.uniformserver.com/ZeroXI_documentation/

 Install unizerver on your desired location.

After the installation of uniserver, we now have our php.exe, it can be found on

C:\UniServerZ\core\php54\php.exe 

We can now proceed on installing composer on windows by providing the link of our php.exe that can be found on our uniserver.


To verify if composer is really installed, We can open our cmd terminal and type "composer"