Quick Contact

    Request and Response in Laravel

    Deal with request and response in Laravel. Sending feature is quite based on input field in code. Let’s see how with quick api, sending of data is done and can set it in Json etc.

    – So Let’s start with code by making input field with respective name, giving title and further if(isset) condition in layout file as create post blade.php placed in views folder as-:

    	@extends (‘layout. main’)
    	@section (‘content’)    
    	< form action=”{{route(‘create-post’)}}” method=”post”>
    	< label>Title< /label>< br/>
    	< input type=”text” name=”title” style=”border: 1px solid black;”/>< br/>
    	< br/>
    	< label>Content< /label>< br/>
    	< input type=”text” name=”content” style=”border: 1px solid black;”/>< br/>
    	< br/>
    	< button> create post< /button>
    	< /form>
    	< hr/>
    	Title: {{$title}}< br/>
    	Content: {{$content}}

    Now also go to routes files i.e. as web.php and write request statement there. As-:

    1. Use Illuminate\http\Request;
    2. Then access the request
    3. And for below the route code write title-:
      		Route :: post(url: ‘/posts/create’, function (Request $request) {
      	   	Return view (view: ‘create-post’, [
      	    ‘page_title’ => ‘create new post’,
      	    ‘Title’ => $request -> input (key: ‘title’),
      	    ‘Content’ => $request -> input (key: ‘content’),

      And run the project and see the results as-:

      Here you can see different functionalities are working.

      For more cleanup-

    1. Add “post title” and “post content” line in web.php file below respective route post syntax, just for the code is smoother.
    2. Next to work with Page numbers as well- go back to posts.blade.php

      And type following code as-:

      Now to make it accessible, go back to web.php file-:

      In web.php main file-> say “page => 1”

      Now for paging system make the paging concept end with adding two main features neede as-:

    1. User can go direct to any page such as page – “10”
    2. If only 1 page exist then beside of going to page 10, page should automatically redirect to page “1” and code will be as-:
      		Route:: get (url:’/posts’, function (Request $request) {
      		$page = ($request -> query (key: ‘page’)? $request -> query (key: ‘page’): 1);
      		Return view (view: ‘posts’, [
      		    ‘page_title’ => ‘posts’,
      		    ‘Page’ => $page
      		}) ->name (name: ‘posts’);

      Next is to create “”API” for request and response.

      So here requests are made which are getting and filtered through API.

      So to test api request write following code and check in browser-:

      		#API Routes:
      		Route::get (url: ’/api/posts’, function (Request $request) {
      	    $format = ($request -> query (key: ‘format’)? $request->query (key: ‘format’): ‘json’);
      		Return ‘api/posts working!’ ;

      – Next thing you have to do is ‘add the response’

      – Add in two ways-:

    1. Include in starting
    2. Add directly access below-:
      Route::get(url:’/api/posts’, function (Request $request)
      $format = ($request -> query (key ‘format’)? $request -> query (key ‘format’): ‘json’);
      If ($format === ‘plain’) {
      Return response (content ‘api/posts working’, status 200) -> header (key ‘Content-Type’, values: ‘text/plain’)

      Now see the response-:

      In API if JSON concept is included then code will be as-:

      Route:: get (url: ‘/api/posts’, function (request $request) {
      $format = ($request->query (key: ‘format’)? $request->query (key: ‘format’): ‘json’);
           If ($format === ‘plain’) {
          Return response (content: ‘api/posts working!’, status:200)
            ->header (key: ‘Content-Type’, values: ‘text/plain’);
      Else if ($format === ‘json’) {
      Return response () -> json ([
        ‘Content’ -> ‘api/posts working!’

    In case of redirection, access to redirect function again includes two steps-:

    1. Include it in starting as-:
    2. Use illuminate\routing\redirection;

    3. 2) Write code below
    4. Now run in browser and see the response.

      So this is how you can work with request and response in different aspect based on the post data and from the input field all by the queries and how you can just sending the raw operator or you can send the JSON output or you can redirect the home page if something is going wrong


    Apply now for Advanced PHP Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.