In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use component development and composer to build classes in the Laravel framework. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Nowadays, the idea of "agile development" and "don't repeat wheels" has become popular in software development. In the project, the secondary development based on the framework has become the preferred scheme. Laravel is an excellent PHP framework, which maintains a leading posture among many frameworks. The outstanding advantages of Laravel are component development, Ioc container and distributed application architecture design. When it comes to PHP component development, we have to mention that composer,composer is specially created for PHP and is the management tool of PHP components. The Laravel framework is an example of component-based development. Later I will demonstrate how to use composer component management tools to manually build the main functions of laravel, including routing functions, controller functions, models, views.
So what is component development?
According to my understanding: for a large project, if each function is divided into different sections at the beginning of the project, the final synthesis is component development. Especially in today's open source society, many people have done a certain section (component) very well, so we can directly embed it into the project (such as some excellent class libraries). Sometimes we can even quickly change the modules in the project without modifying other parts of the code in the system. In order to achieve this goal, we need to follow certain specifications and interfaces. There is a famous PHP-FIG (PHP Framework Interop Group,PHP Framework Interactive Group) in PHP, and its function is to develop a series of PHP development specifications, namely PSR coding specifications. If PHPer adheres to this rule when developing the framework, the various components can be amicably grouped together. PSR mainly includes PSR0-PSR4 documents, in which PSR0 and PSR4 have formulated the Autoloader standard.
Next, manually build the routing function, controller function, model, view in the Laravel framework.
Environmental requirements:
1. Composer (search and install by yourself on the Internet)
2. Above PHP5.3
3. Mysql database
4. Apache or Nginx server
Steps:
1. Project initialization
2. Add routing function
3. Add controller module
4. Add a model
5. Add a view
1. Project initialization
Create a folder in the root directory of the server, named lara, such as my E:\ laravel\ lara (virtual directory), create a composer.json file and enter the following (in order not to reuse composer update, I will load the components I will use later at once)
{"require": {"illuminate/routing": "*", "illuminate/events": "*", "illuminate/database": "*", "illuminate/view": "*"}, "autoload": {"psr-4": {"App\": "app/"}
Composer loads components based on the contents of the composer.json. There are routing components, event components, database components, and view components (where the controller component is already included in the routing component). The configuration of autoload is the psr-4 specification, which means that when we instantiate the User object with "$user = new\ App\ User ()", autoload looks for the User.php file in the app directory. Once you have composer.json, you can load the component. Under window, navigate to the lara directory with cmd and then use the composer update command, similar to linux. After loading, there are the following directories:
2. Configure routing files
We all know that laravel has entry files and routing files. I will follow the example of laravel to create a new public folder under the lara directory and create an entry file under the public directory as follows:
Next, set up the routing file. Create an app folder in the lara directory, another Http folder in that directory, and an routes.php file in the Http directory. The content is as follows (those who have studied the laravel framework should be well aware of it)
Next, access public's index through the server, and the result is as follows:
3. Configure the controller module
According to the laravel directory structure, create a Controllers folder under the app\ Http directory, and then create a WelcomeController.php file, as follows:
Then configure the routing file as follows:
Next, I will visit localhost:8080/lara/public/index.php/welcome (this is my path, which is written according to your own server).
4. Configuration model
First create a database (I call it lara here), and then create a data table students
Then configure the database component under the portal file, and then configure it as follows:
Then configure the database connection, create a config folder under the lara directory, and then create a database.php
You configure the relevant parameters according to your own database.
Next, create the template file, create a Models folder in the app directory, and then create a Student.php in that directory
I didn't create the createTime and updateTime fields, so set $timestamps = false
Finally, we use the Eloquent ORM utilization model and apply the model in the WelcomeController.php file: WelcomeController.php
Visit localhost:8080/lara/public/index.php/welcome again
5. Add view components
The configuration entry file is as follows:
The path is configured according to your own computer.
Then create the lara\ storage\ framework\ views\ directory and the E:\ laravel\ lara\ resources\ views directory to store the compilation files and template files, respectively. Use the view file in the WelcomeController.php configuration:
Read a row of data to the view file welcome
Then create a welcome.blade.php in the lara\ resources\ views directory with the suffix .blade.php, otherwise you won't recognize it. The code is as follows:
{{$data ['name']}} {{$data [' age']}}
Mainly do not surround the code with php tags
Last visit to localhost:8080/lara/public/index.php/welcome
This is the end of this article on "how to use component development and composer to build classes in Laravel framework". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.