miladrahimi / phprouter

PhpRouter is a full-featured yet very fast HTTP URL router for PHP projects
MIT License
195 stars 17 forks source link

Linking Middleware to an external file #17

Closed mbamber1986 closed 5 years ago

mbamber1986 commented 5 years ago

Hello hope this can get answered i have recently found your script and i think its amazing my issue is

when i place the middleware class within my routing files called web.php it runs the middleware script however if i do the following

$router->get('/users', 'Users@index',\App\Middleware\AuthMiddleware::class);

im getting this

Fatal error: Uncaught MiladRahimi\PhpRouter\Exceptions\InvalidMiddlewareException: Invalid middleware for route: {"name":null,"uri":"\/users","method":"GET","controller":"App\Models\Users@index","middleware":["App\Middleware\AuthMiddleware"],

any help would be greatly appreciated

miladrahimi commented 5 years ago

Hello, Could you please put your codes (middleware, directory structure, etc.) here? There some tests for the middleware https://github.com/miladrahimi/phprouter/blob/master/tests/MiddlewareTest.php and it shows that the router supports middleware classes everywhere.

mbamber1986 commented 5 years ago

image

So following your examples i created a simple redirect with no checking now if i simply include the file

<?php namspace App\Middleware use MiladRahimi\PhpRouter\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface;

class Auth implements Middleware { public function handle(ServerRequestInterface $request, Closure $next) { header("location:/auth"); return $next($request); } }

<?php

use MiladRahimi\PhpRouter\Router; use Zend\Diactoros\Response\HtmlResponse; use Zend\Diactoros\Response\JsonResponse;

$router = new Router('', 'App\Models'); $router->get('/auth', function () { return 'OK'; });

$router->get('/', function () { return '

This is homepage!

'; });

$router->get('/users', 'Users@index',App\Middleware\Auth::class); $router->post('/users/Store', 'Users@Store'); $router->dispatch();

so after a bit of experimenting last night i found that if i removed the namespace and dont call by namespace and use and include file directly it calls the middleware

as i saiid including files works calling namespace method doesnt even if i was to call App\Middleware\Auth and then call the middleware as Auth::class

thanks again

miladrahimi commented 5 years ago

Could you explain more, please? Is there still an issue? If you use composer autoload no need to require/include anything manually.

mbamber1986 commented 5 years ago

yes theres still an issue i have used dump autoload and it still causes this issue thanks fir your response here is an image of my strucutre

image

here is a screen of my web.php image

here is a pictiure of my auth middleware script

image

Now if i run it usingh the include file as above it will call the middleware no problem

now when i add namespace App\Middleware to Auth.php and use App\Middleware\Auth in web.php

i ghet this error

image

web.php image

miladrahimi commented 5 years ago

The issue is not about PhpRouter, it belongs to abusing PHP namespaces! The middleware you have implemented has no namespace and you should add it this way:

<?php

namespace App\Middleware;

...
mbamber1986 commented 5 years ago

yes theres still an issue i have used dump autoload and it still causes this issue thanks fir your response here is an image of my strucutre

image

here is a screen of my web.php image

here is a pictiure of my auth middleware script

image

Now if i run it usingh the include file as above it will call the middleware no problem

now when i add namespace App\Middleware to Auth.php and use App\Middleware\Auth in web.php

i ghet this error

image

web.php image

mbamber1986 commented 5 years ago

yep ive already done that

image

and still getting same error

miladrahimi commented 5 years ago

Now if you check your IDE hints, Closure class is declared under \ namespace, so you have to add a use statement for it or you can add \ before it to use it. \Closure

mbamber1986 commented 5 years ago

I.will look at this later thanks for your help I'll get back to you later today

On Tue, 15 Oct 2019, 09:39 Milad Rahimi, notifications@github.com wrote:

Now if you check your IDE hints, Closure class is declared under \ namespace, so you have to add a use statement for it or you can add \ before it to use it. \Closure

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/miladrahimi/phprouter/issues/17?email_source=notifications&email_token=AHKAGFADZHNBGDIFGIYWES3QOV6ULA5CNFSM4JAVOVJ2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBH5YKY#issuecomment-542104619, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHKAGFGYJNFPLDLZY3SIMETQOV6ULANCNFSM4JAVOVJQ .

mbamber1986 commented 5 years ago

Ok so thanks for your support andas i said its an amazing script you have wrote kudos to you the /closure worked and thanks for that peice of informarmation

mbamber1986 commented 5 years ago

sorry i do have one question with this script does it have the ability to use the name as a url forwarder like laravel does when it used router(user.post) for example

miladrahimi commented 5 years ago

Yes, https://github.com/miladrahimi/phprouter#route-name