TypeRocket / core

TypeRocket core source files where all the magic lives.
https://typerocket.com
36 stars 21 forks source link

How to run middleware for all requests? #55

Closed msieracki closed 5 years ago

msieracki commented 5 years ago

Hello

I try to create a middleware that runs for all requests, and not only for certain types. Is it possible to do that?

kevindees commented 5 years ago

Hey @msieracki

Yes. Just add the middleware to the resourceGlobal and hookGlobal group.

msieracki commented 5 years ago

Thanks for your answer.

This solution doesn't work for me, I have something like this:

namespace App\Http\Middleware;
use \TypeRocket\Http\Middleware\Middleware;
use TypeRocket\Core\Config;
class Jmsall extends Middleware
{
    public function handle()
    {
        $request = $this->request;
        $response = $this->response;
        var_dump(get_locale());
        add_filter('locale', [$this,'language'],1,1);
        var_dump(get_locale());
        die();
        $this->next->handle();
    }
    public function language($locale) {
        $locale = "pl_PL";
        return $locale; 
    }
}
class Kernel extends \TypeRocket\Http\Kernel
{
    public $middleware = [
        'hookGlobal'        => [Jmsall::class],
        'restApiFallback'   => [Jmsall::class],
        'resourceGlobal'    => [Jmsall::class],
        'user'              => [IsUserOrCanEditUsers::class ],
        'post'              => [OwnsPostOrCanEditPosts::class],
        'comment'           => [OwnsCommentOrCanEditComments::class ],
        'option'            => [CanManageOptions::class ],
        'term'              => [CanManageCategories::class ],
        'contant_query'     =>  [Jmsall::class],
    ];
}

it "dies" only for some pages. I want to catch all requests - the whole WordPress.

kevindees commented 5 years ago

Hey @msieracki

TypeRocket does not provide a feature to do that with middleware for all of WordPress right now. You may need to have your code applied to the init WordPress hook to get the effect you are looking for.