In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge of this article "how to use the Laravel framework to deal with user requests", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the Laravel framework to deal with user requests" article.
1. Request object Request
Request contains some information requested by the user. To use this object, you first need the use Illuminate\ Http\ Request class, and then pass the object in the parameter.
Public static function getRequest (Request $request) {/ / get the request type echo "request type". $request- > method (). "; / determine the request type if ($request- > isMethod ('POST')) {} / / request url echo" url: ". $request- > url (); / / determine whether the request path matches if ($request- > is ('* / index')) echo 'this is the home page'; / / get the value if ($request- > has ('val')) {var_dump ($request- > input (' val'));}}
2. Response object Response
You can return the response through the return statement. When you return an array, laravel automatically converts it to JSON format. If you need to convert some data to JSON, you can use response ()-> json ():
Return response ()-> json ($data)
The response () method supports custom status codes and response headers:
Return response ($data, 200)-> header ('Content-Type',' text/plain')
Response ()-> download ($path,$name) causes the browser to download the file of the specified path:
Return response ()-> download (storage_path ('app/photo/test.jpg'),' test picture .jpg')
3. Redirect
Redirect the page through the redirect () function
/ / redirect to named route with parameter return redirect ()-> route ('redirect', [' name'= > 'tory']); / / redirect to route with one-time Sessionreturn redirect (' redirect')-> with ('msg','redirect'); / / redirect to controllerreturn redirect ()-> action (' Login@redirect'); / / redirect to previous interface return redirect ()-> back ()
Through with, you can send data to the page through session, and then get the data through Session::get ('msg'), which is also a more secure way to transfer data when jumping between two pages.
4. Middleware
Laravel provides a middleware mechanism to filter the user's request request and process it before returning response. This mechanism is also found in nodeJS's express framework and is called an interceptor, which filters users' requests and then forwards them to the application Application. The middleware files are stored in the app/Http/Middleware directory, including authentication, CSRF protection, and so on. For example, authentication verification middleware will verify whether the user is authenticated (such as login), if the user is not authenticated, the middleware will redirect the user to the login page, and if the user has been authenticated, the middleware will allow the request to proceed to the next step.
4.1. Define middleware
Create a new php class under the directory of the middleware to intercept the request for the activity page, and redirect the request for the activity to the noActivity route before the specified date is reached:
Namespace App\ Http\ Middleware;use Closure;class Activity {public function handle ($request,Closure $next) {/ / if the time is less than the specified time, jump to the noActivity routing if (time () 2)
4.2. Register routing
The Kernel class of the middleware registered in the app/Http/kernel.php file, which has three properties:
The $middleware attribute is used to define the global middleware, and each http request needs to pass through the global middleware.
The $middlewareGroups attribute is used to define middleware groups. For example, if you need to specify multiple middleware for a route, it is too troublesome to specify them separately. You can put them in a middleware group. For example, the middleware group api includes two middleware, throttle:60,1 and bindings
'api' = > [' throttle:60,1', 'bindings',]
The $routeMiddleware property is used to register the allocation middleware to the specified route.
To register the middleware, you need to specify a key value for the middleware, which is used to assign the middleware to the specified route, and corresponds to the path of the middleware.
Protected $routeMiddleware = ['auth' = >\ Illuminate\ Auth\ Middleware\ Authenticate::class,' auth.basic' = >\ Illuminate\ Auth\ Middleware\ AuthenticateWithBasicAuth::class, 'bindings' = >\ Illuminate\ Routing\ Middleware\ SubstituteBindings::class,' can' = >\ Illuminate\ Auth\ Middleware\ Authorize::class, 'guest' = >\ App\ Http\ Middleware\ RedirectIfAuthenticated::class, throttle' = >\ Illuminate\ Routing\ Middleware\ ThrottleRequests::class 'activity'= >\ App\ Http\ Middleware\ Activity::class / / register activity middleware]
4.3. Use middleware
When defining a route, the middleware is assigned to a route through the middleware () method:
Route::get ('activity','Login@activity')-> middleware (' activity')
5 、 Session
Use $_ SESSION in PHP to store user login information and other data on the server side. Laravel does not use PHP default session, but implements a set of session mechanism. Session is driven by files by default, and can be modified to redis or database in config/session.php.
You can access session in three ways
Access session through request object
Accessed through the helper function session ()
Access through the Session class
/ / use HTTP request sessionecho''; $request- > session ()-> put ('key','value'); echo $request- > session ()-> get (' key'); / / use helper function sessionecho "; session (['key2'= >' value2']); echo" session helper function ".session ('key2'); / / session class Session::put ([' key3'= > 'val3']); echo Session::get (' key3')
Get all the session arrays:
$res=Session::all ()
Delete session:
Session::forget ('key'); / / Delete the specified sessionSession::flushu (); / / Delete all session
Save to the array:
Session::push ('arr','val4'); Session::push (' arr','val5'); var_dump (Session::get ('arr'))
Take the data from the session and delete:
$res=Session::pull ('arr'); / / fetch the data and delete it
Deposit one-time data, which can only be checked out once and then deleted:
Session::flash ('key5','val5'); above is the content of this article on "how to use Laravel framework to handle user requests". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow 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.