Closed mbamber1986 closed 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.
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
Could you explain more, please? Is there still an issue? If you use composer autoload no need to require/include anything manually.
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
here is a screen of my web.php
here is a pictiure of my auth middleware script
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
web.php
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;
...
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
here is a screen of my web.php
here is a pictiure of my auth middleware script
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
web.php
yep ive already done that
and still getting same error
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
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 .
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
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
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