This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Meysam
Meysam

Registering middlewares in Laravel is easy:

simply list the middleware class in the $middleware property of your app/Http/Kernel.php class

or

If you would like to assign middleware to specific routes, you should first assign the middleware a short-hand key in your app/Http/Kernel.php file

But how can this be done in an OctoberCMS plugin? Is the "Routing and initialization" meant to be used in place of Kernel.php to register middlewares? If not, where can a plugin register its own middlewares?

OFFLINE
OFFLINE

Hi

Check out my responsive images plugin for an example on how to register a middleware from a plugin:

https://github.com/OFFLINE-GmbH/oc-responsive-images-plugin/blob/master/Plugin.php#L17

Meysam
Meysam

Thank you. That's great. But how did you find out how you should do that? Where was this documented?

OFFLINE
OFFLINE

It don't think this is documentent on the october site. I just searched for the generic Laravel solution and found answers like this one: http://stackoverflow.com/a/30165313

Edit: I have created a pull request to bring it into the docs: https://github.com/octobercms/docs/pull/167

Last updated

DMeganoski
DMeganoski

I have also added my own middleware to october. It is worth noting that the previously mentioned method does not seem to include the middleware in ALL url's. If I remember correctly, the login page specifically did not, and I needed the logic in the middleware on that page as well.

What I did was create my own extension of Kernal.php, put it in my plugin, and told composer to include it in the autoload.

Then in 'bootstrap/app.php' replace the Kernal with your own.


$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    Company\Plugin\Http\Kernel::class
);

This is not ideal for developing plugins, of course. But will guarantee the middleware is always ran.

Last updated

dai.mike16723
dai.mike16723

DMeganoski said:

I have also added my own middleware to october. It is worth noting that the previously mentioned method does not seem to include the middleware in ALL url's. If I remember correctly, the login page specifically did not, and I needed the logic in the middleware on that page as well.

What I did was create my own extension of Kernal.php, put it in my plugin, and told composer to include it in the autoload.

Then in 'bootstrap/app.php' replace the Kernal with your own.


$app->singleton(
   Illuminate\Contracts\Http\Kernel::class,
   Company\Plugin\Http\Kernel::class
);

This is not ideal for developing plugins, of course. But will guarantee the middleware is always ran.

@DMeganoski , thanks for your comment, I am facing the same issue like you. and I think you are right way to make it, just some pages, not for all, so can make a little detail about the steps of your approach? specially for how to make it into composer autoload? appreciate!

DMeganoski
DMeganoski

Just include the paths to custom classes in autoload in composer.json. It will search these directories for php classes and include them in autoload.

"autoload": {
    "classmap": [
      "plugins/company/plugin/middleware",
      "plugins/company/plugin/http"
    ]
  },

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.