Skip to content

Instantly share code, notes, and snippets.

@salipro4ever
Created October 10, 2018 10:40
Show Gist options
  • Save salipro4ever/03d65123782550c8db01be1e35853b22 to your computer and use it in GitHub Desktop.
Save salipro4ever/03d65123782550c8db01be1e35853b22 to your computer and use it in GitHub Desktop.
class ProjectController extends Controller
{
    /**
     * All of the current user's projects.
     */
    protected $projects;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->projects = Auth::user()->projects;

            return $next($request);
        });
    }
}
@salipro4ever
Copy link
Author

salipro4ever commented Nov 15, 2018

By default, Csrf token on laravel keep changing on session. This snippet, allow it refresh on matched writing requests (POST, PUT ...)

Note that override addCookieToResponse method, csrf_token() will dont work because it is called before new token is generated.
Using once csrf token on ajax is not simple (ref XSRF)

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];

    /**
     * Override to allow refreshing csrf token on matched writing request (only POST, PUT...)
     *
     * @param \Illuminate\Http\Request $request
     * @return bool
     */
    protected function tokensMatch($request)
    {
        if($match = parent::tokensMatch($request)){
            $request->session()->regenerateToken();
        }
        return $match;
    }
}

Laravel stores the current CSRF token in a XSRF-TOKEN cookie that is included with each response generated by the framework. You can use the cookie value to set the X-XSRF-TOKEN request header.

@salipro4ever
Copy link
Author

salipro4ever commented Jan 10, 2019

Method Injection (IoC)

// TheClass
public function doThis(SomeInterface $dependency)
// Must use call
app()->call('TheClass@doThis')
public function doThing(Container $container)
    {
        $thingDoer = $container->make('ThingDoer');

        // Calls the $thingDoer object's doThing method with one parameter
        // ($thing_key) with a value of 'awesome-parameter-here'
        $container->call(
            [$thingDoer, 'doThing'],
            ['thing_key' => 'awesome-parameter-here']
        );
    }

https://laracasts.com/discuss/channels/laravel/understanding-method-injection
https://stackoverflow.com/questions/27930189/laravel-5-method-injection

@salipro4ever
Copy link
Author

function debug_string_backtrace() {
        try {
            throw new \Exception();
        } catch (\Exception $e) {

            dump($e->getTraceAsString());
        }


    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment