In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of Laravel framework routing and how to apply MVC. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Routing
The function of routing is to forward the user's different url requests to the corresponding program for processing. The route of laravel is defined in the routes folder, and four routing files are provided by default, in which the web.php file defines the basic page request.
1.1, basic routin
The most basic routing requests are get and post requests, and laravel defines different request methods through the Route object. For example, define a get request whose url is' req' 'and return the string' get response':
Route::get ('req',function () {return' get response';})
When I request http://localhost/Laravel/laravel52/public/req in the form of get, the response is as follows:
Similarly, when defining a post request, use Route::post (url,function () {})
1.2. Multi-request routing
If you want to handle multiple request methods in the same way, you can use match or any:
Use match to match the corresponding request method, for example, when a req2 is requested with get or post, match response is returned:
Route::match (['get','post'],' req2',function () {return 'match response';})
Any will match any request method, for example, request req3 in any way and return any response:
Route::any ('req3',function () {return' any response';}); 1.3, request parameters
Required parameters: when a request is sent with parameters, it can be received in the route, enclosing the parameters in curly braces and dividing them with /, for example:
Route::get ('req4/ {name} / {age}', function ($name, $age) {return "name}, {$age} years old.";})
The parameter is passed when requested with get, and the result is as follows:
Optional parameters: the above parameters are required. If a parameter is missing, an error will be reported. If you want a parameter to be optional, you can add one to it and set the default value. The default parameter must be the last parameter, otherwise it cannot be identified in the middle:
Route::get ('req4/ {name} / {age?}', function ($name, $age=0) {return "name}, {$age} years old.";})
Regular check: the parameters in the request can be verified through where
Route::get ('req4/ {name} / {age?}', function ($name, $age=0) {return "name}, {$age} years old.";})-> where (['name'= >' [A-Za-z] +', 'age'= >' [0-9] +'])
Sometimes our routing may have multiple levels, such as defining a first-level routing home, under which there is a secondary routing article,comment, etc., so we need to put article and comment into the home group. Add the prefix home to the routing article through the array key prefix:
Route::group (['prefix' = >' home'], function () {Route::get ('article', function () {return' home/article';});})
This way the route can be accessed through home/article.
1.5. Route naming
Sometimes you need to give a name to a route, and you need to use the as array key to specify the route name when defining the route. For example, if you name the route home/comment comment, you can use the name comment of the route when generating url and redirection:
Route::get ('home/comment', [' as'= > 'comment',function () {return route (' comment'); / / generate url} corresponding to comment through the route function])
Export to http://localhost/Laravel/laravel52/public/home/comment
2. Controller
Route routing only allocates the request to jump, and the specific business logic needs to be handled by the controller, which is generally encapsulated into a php class. The files for the controller are usually placed under the app/Http/Controlers folder. For example, create a new LoginController class that inherits from Controller and define a checkLog method to respond to login requests.
Namespace App\ Http\ Controllers;class LoginController extends Controller {public function checkLog ($name) {return $name.' Login succeeded';}}
Assign the login request to the checkLog method in route.php:
Route::get ('login/ {name}', 'LoginController@checkLog')
Similarly, you can give the controller a name for routing, such as login:
Route::get ('login/ {name}', ['uses'= >' LoginController@checkLog','as'= > 'login']); 3. View
Controller is responsible for handling the logic of the application, and the view View is responsible for the display of the application, which reflects the separation of different logic in the MVC. Views are generally located in the / resource/views directory, usually a controller file corresponds to a view folder, so the view I created is: views/Login/login.blade.php. The blade file is a template engine for laravel, which is compiled and stored as PHP. It contains the HTML language, in which you can use PHP directly, such as login.blade.php:
Login interface {{$name}} login succeeded
Call the view in the checkLog method of controller and pass in the parameters:
Public function checkLog ($name) {return View ('Login/login', ["name" = > $name]);} 4, template
Interact with the tables in the database through Models in mvc, and each database corresponds to an Model template. Laravel does not define a models directory, so you can generally create a new models folder under the app directory to store template files. For example, define a Student template and specify the table name and primary key:
Namespace App\ Models;use Illuminate\ Database\ Eloquent\ Model;class Student extends Model {protected $table='student'; protected $primaryKey='id';}
Query all of the following through the Student template in controller:
Namespace App\ Http\ Controllers;use App\ Models\ Student;class Login {public static function showDB () {$table=Student::all (); dd ($table);}} these are all the contents of the article "Laravel Framework routing and how to use MVC". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.