.
Two commonly used methods for a request-response between a client and server are: GET and POST.
GET - Requests data from a specified resource
POST - Submits data to be processed to a specified resource
1) The file routes.php represents the connection between your site's URLs and the functions that contain application logic for your site.
2) Using a text editor (e.g. notepad++), open {laravel root}\application\routes.php
3) Look at code lines no. 14 to 18 above.
4) Copy the codes.
5) Locate the following code lines. (e.g. line no. 35 to 38)
6) Paste the copied codes to line beneath the above codes. (e.g. line no. 39 onwards.)
7) Browse your site's URLs, e.g. http://mysite.dev/hello
8) The newly inserted codes (e.g line no. 39 to 41) belongs to Get Method called 'hello'. Whenever the URL is requested, Laravel will call the method. The method contains a return statement that returns the string "Hello World!".
9) Create a new folder and file --> {laravel root}\application\views\hello\getpage.blade.php Type the following codes to getpage.blade.php:
9a) Take note that this HTML codes include a FORM that will send a POST METHOD request using an action parameter "hello". 10) Edit routes.php. Comment line 41 and insert line 42. Instead of returning a string, we instruct the server to return a view of the above file.
11) Browse http://mysite.dev/hello. You get a view of the file getpage.blade.php . Notice that the URL doesn't change.
12) Enter the username.
13) Click Submit. At this point, you may get a Server Error:404 (Not Found).
14) What happened?
14a) When you submit the form, the form send a POST request to the server.
14b) The server refers to the routes.php to look for the POST route.
14c) At the moment, POST method has not yet been defined. So, routes.php respond with Application 404 error.
14d) An Error Response return an Error Page located at {laravel root}\application\views\error\404.php
15) Insert a POST route. Edit routes.php. Insert new lines (e.g. No 44 to 47)
16) Repeat steps 11 to 13. Instead of a 404 error page, you should get a blank page response now. That means, the post method is recognized but since it is an empty method, the browser doesn’t get anything in return.
17) To complete this tasks, create a page to respond to Submit event. Create a new file --> {laravel root}\application\views\hello\postpage.blade.php Type the following codes to postpage.blade.php:
18) Edit routes.php. Insert a new line in the Route::post method (e.g. Line 46)
18) Repeat steps 11 to 13. The server now responds with a Welcome page. The greeted name is picked up from the form's username in the previous page using PHP $_POST variable.
19) Using Laravel’s Input Class
Open {laravel root}\application\views\hello\postpage.blade.php
Edit the Welcome statement as below. You get the same output based on Laravel style.
20) TRY: Create a bye GET method to make a view of a bye form which will request a bye POST method and make a bye page.
.
No comments:
Post a Comment