۳ امیر رحمانی
بنظرم بهتره برای میدلور‌ها بصورت method chainig رفتار کنیم
امیر صالحی حل شده توسط امیر صالحی

بنظر من بهتره میدلورهامون رو بصورت method chainig به روتهامون بدیم که خوانایی کد ما بالاتر میبره :

به این صورت

 

    private static $middleware;
    public static function add($methods, $uri, $action = null)
    {
        $methods = is_array($methods) ? array_map('strtolower', $methods) : [$methods];
        self::$routes[] = ['methods' => $methods, 'uri' => $uri, 'action' => $action];
        return new self;
    }
    public function middleware($middlewareName){
        self::$middleware = is_array($middlewareName) ? $middlewareName : [$middlewareName];
        self::$routes[]['middleware']=self::$middleware;
    }

برای تعریف میدلور هام اینجوری عمل میکنیم

Route::add(['get'],'/home',function(){
    echo "Welcome";
})->middleware(BlockFirefox::class);
#exm2
Route::get('/product/list','product@list')->middleware([BlockFirefox::class,BlockIe::class])
#

 

اصلاح کد بالا

    private static $middleware;
    public static function add($methods, $uri, $action = null)
    {
        $methods = is_array($methods) ? array_map('strtolower', $methods) : [$methods];
        self::$routes[] = ['methods' => $methods, 'uri' => $uri, 'action' => $action];
        return new self;
    }
    public function middleware($middlewareName){
        self::$middleware = is_array($middlewareName) ? $middlewareName : [$middlewareName];
        self::$routes[count(self::$routes)-1]['middleware']=self::$middleware;
    }

 

امیر رحمانی ۰۱ خرداد ۱۴۰۰، ۰۹:۲۵

سلام و احترام

بله یه چیز شبیه به routeی که توی لاراول استفاده میشه

بهترین پاسخ
امیر صالحی ۰۱ خرداد ۱۴۰۰، ۱۷:۱۶

Route::get('/todo/list','TodoController@list')->middleware([BlockChrome::class,BlockIE::class]);
show this error:
Expected type 'object'. Found 'null'.intelephense(P1006)

-------------

The error Expected type 'object'. Found 'null'. intelephense(P1006) occurs because the middleware() method in the Route class is being called on null. This happens because the Route::get() method does not return an instance of the Route class, but instead returns null.

Problem: 

In the Route class, the get() method (and other HTTP methods like post(), put(), etc.) does not return the Route instance. Instead, it simply calls the add() method, which does not return anything.

Fix:
Update the get() method (and other HTTP methods) in the Route class to return the Route instance. Modify the methods as follows:

<?php
public static function get($uri, $action = null)
{
    return self::add('get', $uri, $action); // Return the instance
}
public static function post($uri, $action = null)
{
    return self::add('post', $uri, $action); // Return the instance
}
public static function put($uri, $action = null)
{
    return self::add('put', $uri, $action); // Return the instance
}
public static function patch($uri, $action = null)
{
    return self::add('patch', $uri, $action); // Return the instance
}
public static function delete($uri, $action = null)
{
    return self::add('delete', $uri, $action); // Return the instance
}
public static function options($uri, $action = null)
{
    return self::add('options', $uri, $action); // Return the instance
}

Explanation:
The add() method in the Route class already returns a new instance of the Route class. By ensuring that the get(), post(), and other methods return the result of add(), you enable method chaining (e.g., calling middleware() after get()).

 

After applying the fix, the following line will work without errors:

<?php
Route::get('/todo/list', 'TodoController@list')->middleware([BlockChrome::class, BlockIE::class]);
علی منظور ۲۱ فروردین ۱۴۰۴، ۱۶:۰۰