PHP code example of te7a-houdini / response-generate

1. Go to this page and download the library: Download te7a-houdini/response-generate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

te7a-houdini / response-generate example snippets


namespace App\Http\Responses;
    
use Illuminate\Contracts\Support\Responsable;
    
class ExampleResponse implements Responsable
{   
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function toResponse($request)
    {
        
    }
}

public function show($id)
{
    $post = Post::find($id);
    
    if (request()->ajax)
    {
        return response()->json(['data' => $post]);
    }
    
    else
    {
        return view('posts.show',compact('post'));
    }
}

public function show($id)
{
    $post = Post::find($id);
    
    return new PostResponse($post);
}

class PostResponse implements Responsable
{   
    protected $post;
    
    public function __construct ($post)
    {
        $this->post = $post;
    }
    
    /**
     * Create an HTTP response that represents the object.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function toResponse($request)
    {
        if ($request->ajax())
        {
            return response()->json(['data' => $this->post]);
        }
        else
        {
            return view('posts.show',$post);
        }
    }
}