Friday, 17 September 2021

Installing XAMP and Creating YII application using PHP , setting database connection and GII control usage

Download xamp software from the website and install in machine by following steps

XAMP software link : https://www.apachefriends.org/download.html

During the installation process, select the required components like MySQL, FileZilla ftp server, PHP, phpMyAdmin or leave the default options and click the Next button. 

  • Uncheck the Learn more about bitnami option and click Next button.
  • Choose the root directory path to set up the htdocs folder for our applications. For example ‘C:\xampp’. I have chosen 'C:\xampp' as my xamp root folder. The installed path of the xamp is taken as root directory (say xamp root directory/folder ) 
  • Click the Allow access button to allow the XAMPP modules from the Windows firewall.
  • After the installation process, click the Finish button of the XAMPP Setup wizard.
  • Now the XAMPP icon is clearly visible on the right side of start menu. Show or Hide can be set by using the control panel by clicking on the icon.
  • To start Apache and MySql, just click on the Start button on the control panel. 
     

control-panel-xampp

Note: Suppose Apache is not starting, it means some other service is running at port 80. In this case, stop the other service temporarily and restart it.


Making server request: Open your web browser and check whether the XAMPP service has properly installed or not. 

Type in the URL: http://localhost. If you are able to see the default page for XAMPP, you have successfully installed your XAMPP Server.


To Check if PHP is Working: All the website related files are organized in a folder called htdocs and then run index.php file by using 

http://localhost/index.php or http://localhost.


Note: For every new website or application, its always better to create a different folder inside htdocs, to keep it organized and avoid confusion.
For example, if we create a folder helloworldapp and then create a file named as ‘helloWorld.php’. All the contents related to it are put inside the folder 
helloworldapp’.

So the root ‘URL’ of the website will be ‘http://localhost/helloworldapp/’. So any home page is accessed by typing the root URL in the browser. To see the output, just type ‘http://localhost/helloworldapp/helloWorld.php’.


Generally web servers look for index file (landing page) namely index.html or index.php in the root of the website folder. Go to /xampp/htdocs/ folder and create a file with .php extension (test.php) and type or copy the below code and save it.
 

<?php
phpinfo();
?>

Now open your browser and go to “http://localhost/test.php” if you see the 
page same as below then PHP has successfully installed. 
 

test-screen-phpinfo

Note: In XAMPP, the configuration files of Apache, MySQL, PHP are located in xampp root folder. For any configuration file changes, you need to restart Apache and MySQL


Path Setting : 


Now PHP path to be set in windows environment variables so that the installed mysql/php or other selected services can be accessed through command prompt.

Right click on MyComputer (This Computer) and choose Properties --> Advanced System settings --> Environment variables

 (or)

Start by typing in Environment Variables in your Search and click on the Edit the system environment variables result.

In the System Properties window click Environment Variables. Make sure that you’re on the Advanced tab.

Scroll down to the Path variable and click Edit.



Click on the Browse button.

Navigate to C:/xampp/php and click OK.

You will now see the path to php inside the window. Click OK to exit.

Restart your Command Prompt window if it’s open and type in:

php -v

You should see the PHP version being displayed.

You can now use php from CMD (Command Prompt)

YII Installation 
Download YII from the https://www.yiiframework.com website
I have used YII framework version 1.1.14 
download the zip folder and extract the contents (Here I renamed the folder name as yii instead of yii-1.1.14) and copy the folder to the XAMP_Root_Directory/htdocs folder.

Now to create the application in YII run the following command

C:\xampp\htdocs\yii\framework>yiic webapp c:\xampp\htdocs\myFirstApp

[Note: My xamp root location is c:\xampp and I have copied the YII folder in htdocs, if you have done the same the above statement executes as it is, otherwise you have change the paths and folder names accordingly].

Now the above statement asks the following query type 'yes' like me :)

Create a Web application under 'C:\xampp\htdocs\myFirstApp'? (yes|no) [no]:yes

A flow statements executes , which means YII creating the application with the folder name as "myFirstApp" in the specified location, i.e., htdocs

Now open the link :http://localhost/myFirstApp in your favorite browser, make sure that the Apache is running in your xamp control panel (my default listening port is 80 so I haven't specified the port number in URL if Apache running on different port then specify the port number in the URL as http://localhost:port_number/application_name )


The application will be opened as follows : 


The default credentials to login are  defined in C:\xampp\htdocs\myFirstApp\protected\components folder UserIdentity.php file.. the default credentials are admin => admin (or) demo => demo (obviously admin is more privileged)

To give database connection, check the file 

C:\xampp\htdocs\myFirstApp\protected\config\main.php and modify the connection parameters accordingly i.e., edit the following part


Uncomment the above and specify the connection strings accordingly( If your database is mysql make sure the mysql running in the XAMP control panel, and also path settings done for mysql before procedding)



Now after connection open the "gii" to create models, controllers and views . To set the gii password in the same C:\xampp\htdocs\myFirstApp\protected\config\main.php file edit the following part, If it is commented, uncomment the part.. 


Here I have given the password as application name i.e.,
myFirstApp , once this is done open the following link in your browser

http://localhost/myFirstApp?r=gii

The following screen will be displayed, now provide the password i.e., myFirstApp


and click Enter button


Now you can generate the model/crud/controller/form/model or module whatever you want, but make sure the DB Connection is proper before performing this operation.

Happy Coding!!!






.

No comments:

Post a Comment