PHP code example of oguz-yilmaz / trammel

1. Go to this page and download the library: Download oguz-yilmaz/trammel 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/ */

    

oguz-yilmaz / trammel example snippets


// app/Exception/Handler.php
use Oguz\Trammel\Exception\BaseHandler;

class Handler extends BaseHandler
{
    // if class has render method, remove it
}



namespace App\Exceptions\Handlers;

use Symfony\Component\HttpFoundation\Response;
use Oguz\Trammel\Handlers\AjaxHandler;
use Illuminate\Http\Request;
use Throwable;

class CustomAjaxHandler extends AjaxHandler
{
    protected function handle(Request $request, Throwable $exception): Response
    {
        return response()->json([
            'success' => true
        ]);
    }
}

namespace App\Exceptions;

use App\Exceptions\Handlers\CustomAjaxHandler;
use Oguz\Trammel\Exception\BaseHandler;

class Handler extends BaseHandler
{
    protected array $handlers = [
        CustomAjaxHandler::class
    ];
    
    //...
}
 


namespace App\Http\Controllers;


use Illuminate\Validation\ValidationException;
use Illuminate\Http\Request;
use Exception;

class IndexController extends Controller
{
    // $request->isJson() === false && $request->ajax() === false
    public function index()
    {
        throw new Exception('Generic exception that will be passed to Laravel');
    }

    // $request->isJson() === false && $request->ajax() === false
    public function validation(Request $request)
    {
        throw ValidationException::withMessages(['message' => 'Validation Exception']);
    }

    // $request->ajax() === true
    public function ajax(Request $request)
    {
        throw new Exception('AJAX Exception');
    }

    // $request->isJson() === true
    public function json(Request $request)
    {
        throw new Exception('JSON Exception');
    }

    // $request->ajax() === true
    public function ajaxValidation(Request $request)
    {
        throw ValidationException::withMessages(['message' => 'AJAX Validation Exception']);
    }

    // $request->isJson() === true
    public function jsonValidation(Request $request)
    {
        throw ValidationException::withMessages(['message' => 'JSON Validation Exception']);
    }
}