【问题标题】:Slim framework htaccess超薄框架 htaccess
【发布时间】:2018-03-08 17:01:17
【问题描述】:

我使用的是 Slim 框架,htaccess 是这样的:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

所以所有请求都会转到 index.php,然后路由路径。我的网站上有一些类别页面,例如:

/category/1/apples
/category/2/pears
/category/3/oranges

我希望这些类别也可以通过以下方式访问:

/apples
/pears
/oranges

请帮助我编辑 htaccess 以实现此目的。

我发现这个问题几乎可以为我解答,但不完全是:Working with Slim Framework and htaccess

【问题讨论】:

    标签: apache .htaccess mod-rewrite slim


    【解决方案1】:

    您的 .htaccess 文件看起来不错。 Slim Framrwork 负责进一步的路由。

    例如routes.php

    <?php
    
    use Slim\Http\Request;
    use Slim\Http\Response;
    
    $app->get('/category/{id}/{name}', function(Request $request, Response $response, $args) 
       return $response->withJson($args);
    });
    
    $app->get('/apples', function(Request $request, Response $response, $args) 
       return $response->write('apples');
    });
    
    $app->get('/pears', function(Request $request, Response $response, $args) 
       return $response->write('pears');
    });
    
    $app->get('/oranges', function(Request $request, Response $response, $args) 
       return $response->write('oranges');
    });
    

    如果您需要更多通用路线,请尝试以下操作:

    $app->get('/category/{id}/{name}', function(Request $request, Response $response, $args) 
       return $response->withJson($args);
    });
    
    $app->get('/{category}', function(Request $request, Response $response, $args) 
       return $response->withJson($args);
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多