PHP Institute of eight days to build a framework for environmental day4

 

First, select the frame

PHP currently has four mainstream framework, Yii, ThinkPHP, laravel and codeigniter.

PHP framework with other frameworks language is somewhat different. PHP frameworks are large and some of the big stuff.

Unlike the Java language, Spring responsible for dependency injection, SpringMVC responsible controller, Mybatis responsible for database-related, SpringSecurity responsible authority related to perform their duties. But SpringBoot has gradually become a foundation-frame.

Due to time constraints, I did not choose each frame a try. From their official online face look design, I personally think Yii considered outstanding, but here choose to study laravel.

why? Because the number of star laravel on GitHub completely rolled above the other three frames. In addition, I appreciate the self-introduction laravel: The PHP Framework for Web Artisans, translates Web PHP framework craftsmen. And it has repeatedly stressed its syntax is very elegant.

 

Second, the development environment to build

Homestead

laravel recommended laravel / homestead as a development environment.

What homestead is it?

The official explanation: Laravel Homestead is an official pre-packaged Vagrant box, it provides a perfect development environment for you, you need to install PHP on the local machine, web servers and other server software.

So it's essentially a development environment provides a virtual machine.

It depends on Vagrant .

What is a vagrant?

Vagrant is a tool to build and manage virtual machine environment in a single workflow.

Domestic developers know Vagrant's not much, but they know a lot of Docker. In many cases, both the nature of the same.

Vagrant suitable to manage virtual machines, while docker suitable for management applications.

Since Vagrant is to build and manage a virtual environment tool, you also need a virtual environment, homestead is the virtual environment. Virtual environment requires a virtual machine, so it needs to select a virtual machine software. vmware or VirtualBox .

This software uses two almost identical, the most fundamental difference is charged vmware, virtualbox free.

vmware stable, high performance, multi-function, but the volume is large. virtualbox smaller number.

I use vmware from 16 years to now nearly four years, but this year I will not use vmware, but turned virtualbox. There are two reasons, you can refer to:

1. Before I used to use a virtual machine environment to develop, so you can isolate Folio machine environment and development environment. But later I found that when I use the computer almost all the time to develop. Put the machine together environment and development environment used.

2. costs, vmware fee is too expensive.

Now use virtualbox, mainly to test some software in centos above.

So for the scene Framework development environment, I recommend using virtualbox.

Since we are using shell access to the virtual environment the way, so it needs to have a shell tool. The official recommended to use git bash.

To sum up, Homestead need to install the following software:

  • git bash (in fact, this one non-essential)

  • Hyper-V (VM relies on the software, the general mainstream computer will have, if not to install Microsoft's official website)

  • VMware or Virtual Box

  • Vagrant

The installation environment will be very complicated, and even some students learning PHP will spend several days on the installation environment this step.

Since so much trouble, why use Homestead it? We build on day 0 development environment is not good? Here is the official reason (explain):

https://learnku.com/articles/4668/why-do-you-have-to-use-homestead-to-develop-laravel-applications

Official parlance, Homestead can get rid of a lot of trouble, although Homestead itself a lot of trouble.

Git and install VirtualBox

The two software installation and use are relatively simple. Not repeat them here.

The future may write related articles.

It is recommended Liao Xuefeng teacher git tutorials: https://www.liaoxuefeng.com/wiki/896043488029600 . As for VirtualBox, all without tutorials. It will be a multi-purpose use.

Installation Homestead

Open git bash, enter the command:

vagrant box add laravel/homestead

The role of this command is to install the latest version of the homestead.

But the domestic network download speed is very slow, I tried several times before download. You can also try more than once.

In addition to the installation command, you can also download the file to a local box before installation. But this may be other problems. I do not recommend this way to install.

Used after installation vagrant box listto see if the installation was successful.

Cloning down homestead directory.

git clone https://github.com/laravel/homestead.git

After the download directory will see Homestead in the current folder.

Homestead has a init.sh file folder in the root directory, execute it.

bash init.sh

Completion of the above steps, and the process does not appear in error, it proved no problem.

Next, go to Homestead configuration link.

Configuration Homestead

There is a Homestead.yaml file in the root directory of Homestead, Homestead placed inside the main configuration.

Along the following lines:

---
ip: "192.168.10.10"
memory: 2048
cpus: 2
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
  - ~/.ssh/id_rsa

folders:
  - map: E:/project3/php/laravel
    to: /home/vagrant/code

sites:
  - map: homestead.test
    to: /home/vagrant/code/Laravel/public

databases:
  - homestead

features:
  - mariadb: false
  - ohmyzsh: false
  - webdriver: false

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

authorize keys and public key configuration options are git, fill in the normal file location.

map represents the list of options under the folders you want to map our real host, to represent directories Homestead virtual machine to be mapped.

The above configuration, the present machine is to E:/project3/php/laravelbe mapped to /home/vagrant/code.

See here, you should be very clear, Homestead is a linux virtual machine.

sites option represents the URL mapping, map indicates the address to be entered in the browser, to indicate the file directory virtual machine to be mapped.

Then modified the sites, but also to modify the hosts file.

windows hosts file in C:\Windows\System32\drivers\etc\the next.

If you do not want to modify the hosts file, you can. Then you need to enter the ip address in the browser, such as the above configuration, the browser input 192.168.10.10is the same.

databases option represents the database. The default user name is mysql database homestead, the password is secret

After homestead.yaml file configured, the homestead folder root open git bash, execute vagrant upthe command to start vagrant virtual machine.

If the whole process is not a problem, then build a development environment even better.

Creating laravel project

Do this step, you can finally create laravel project.

There are two ways to create laravel project. One is directly created locally, one is to enter the virtual machine creation. Its essence is the same.

Step virtual machines created as follows:

Implementation of vanrant sshlanding a virtual machine.

Change to the directory mapping, the execute composer create-project laravel/laravel Laravel --prefer-distcommand, create a file called Laravel of laravel project.

Enter in your browser http://192.168.10.10/ or enter your URL mapping configurations, see Laravel the default interface even if successful.

 

Third, the directory structure

It has the following directory under the root directory:

  • app Directory contains the core code of the application.

  • bootstrapDirectory contains guidance framework app.phpdocument.

  • config Directory, as the name implies, the application contains all the configuration files.

  • database Directory containing the data files as well as fill and migration model factory class.

  • publicDirectory contains an entry file index.php, it is the entry point into the application of all requests.

  • resources Directory contains the views and uncompiled resource files (such as LESS, SASS, or JavaScript).

  • routes Directory contains all the route definition application.

  • storage Blade template directory contains the compiled, session session generated files, cache files and other files generated skeleton.

  • tests Directory contains the automated test file.

  • vendorDirectory contains all the Composer dependencies.

We are placed in the code is almost app directory, so look at the main app directory.

Controller and Middleware

We need to do a RESTful style API service, then the next app catalog for us the most important directories is Http directory.

Under Http directory only Controller and Middleware two folders and a Kernel.php file.

Controller folder is the controller, Middleware middleware. Kernel is used to manage middleware.

The concept of the controller, we should all understand, not much to say.

The concept of middleware, Java programmers may no impression, you can first be understood as a filter.

As a service MVC framework, which is almost all the core.

routing

In addition, we also need to pay attention to a file in the root directory /routes/api.php. This file is used to manage the route.

Used Spring Framework students should know, Spring routing and control is put together. This is the style of the older generation server.

laravel Spring practice is elegant than many of its front-end and mainstream ideology is very consistent. Routing and separated from the controller. Jump to by the routing controller.

There is also a directory under routes web.php file, which manages this page routing. This is the most traditional server-side MVC thinking, request immediate return html practices. In today's era of rampant large front-end, we have separated the front and back, so this is not visible.

Guess you like

Origin www.cnblogs.com/luzhenqian/p/11427220.html