Slim Framework路由返回,除了“获取”命名的路由

时间:2019-01-26 17:28:12

标签: php slim-3

我使用:细长骨架(github上有Rob Allen先生的脚手架)

在/ projects分组的路由下,只有“ get” 方法路由可以正常工作,但其余所有返回page not found error。这些路由还具有 auth中间件+ cors中间件(Cors中间件,取自Slim Framework v3文档)。

这是我的../src/routes.php文件:

use Slim\Http\Request;
use Slim\Http\Response;

// Routes

// Administration
$app->group('/admin', function() use ($app) {

    // Dashboard
    $app->get('/dashboard', 'App\Controllers\Admin\Dashboard:index');

    // Projects and images management
    $app->group('/projects', function() use ($app){


        // Projects Actions
        $app->get('/list', 'App\Controllers\Admin\ProjectManagement:index');
        $app->post('/add', 'App\Controllers\Admin\ProjectManagement:add');    # NOT WORKING
        $app->get('/id/{id}', 'App\Controllers\Admin\ProjectManagement:find');   
        $app->put('/edit/{id}',  'App\Controllers\Admin\ProjectManagement:edit');   # NOT WORKING
        $app->delete('/remove/{id}', 'App\Controllers\Admin\ProjectManagement:remove'); # NOT WORKING

        // Project Images Actions
        $app->get('/{pid}/images', 'App\Controllers\Admin\ProjectImageManagement:attachments');
        $app->post('/{pid}/images/attach', 'App\Controllers\Admin\ProjectImageManagement:attach');


        // Project's Image management
        $app->get('/{pid}/images/id/{id}', 'App\Controllers\Admin\ProjectImageManagement:find');
        $app->put('/{pid}/images/edit/{id}', 'App\Controllers\Admin\ProjectImageManagement:edit');
        $app->delete('/{pid}/images/remove/{id}', 'App\Controllers\Admin\ProjectImageManagement:removeImage');


        /**
         * Project's Image Sort Order
         *
         * Additional Info:
         *
         * GET    /{pid}/images     Retrieves current sort order beforehand
         *
         */

        $app->put('/{pid}/images/order/{id}/resort', 'App\Controllers\Admin\ProjectImageManagement:sortOrder');

    });


    // Page management
    $app->group('/pages', function() use ($app) {

        // About Page
        $app->get('/about/content', 'App\Controllers\Admin\PageManagement:aboutPage');
        $app->put('/about/content/update', 'App\Controllers\Admin\PageManagement:updateAbout');

        // Contact Page
        $app->get('/contact/content', 'App\Controllers\Admin\PageManagement:contactPage');
        $app->put('/contact/content/update', 'App\Controllers\Admin\PageManagement:updateContact');

    });

    // Settings
    $app->group('/settings', function() use ($app) {

        // Account
        $app->get('/account/details', 'App\Controllers\Admin\Settings:accountDetails');
        $app->post('/account/details/apply', 'App\Controllers\Admin\Settings::applyAccountSettingChanges');
    });



})->add($auth);



// Auth
$app->get('/auth/point', 'App\Controllers\AuthController:checkPoint');
$app->post('/auth/login','App\Controllers\AuthController:login');
$app->get('/auth/logout', 'App\Controllers\AuthController:logout');


// Guest
$app->get('/about', 'App\Controllers\Guest\PageContents:about');
$app->get('/contact', 'App\Controllers\Guest\PageContents:contact');
$app->get('/works', 'App\Controllers\Guest\ProjectExplorer:projects');

这是我在/ public目录下的.htaccess:

 <IfModule mod_rewrite.c>
  RewriteEngine On

  # Some hosts may require you to use the `RewriteBase` directive.
  # Determine the RewriteBase automatically and set it as environment v 
 ariable.
  # If you are using Apache aliases to do mass virtual hosting or 
    installed the
  # project in a subdirectory, the base path will be prepended to allow proper
  # resolution of the index.php file and to redirect to the correct URI. It will
  # work in environments without path prefix as well, providing a safe, one-size
  # fits all solution. But as you do not need it in this case, you can comment
  # the following 2 lines to eliminate the overhead.
  RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
  RewriteRule ^(.*) - [E=BASE:%1]

  # If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
  # absolute physical path to the directory that contains this htaccess file.
  # RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

我还添加了全部允许,但结果是相同的。但是,如果我做错了,为什么在“ / projects”之外创建的所有其他路由都能正常工作。例如,与身份验证相关的路由可以很好地发挥作用。

我知道,我必须看到“不允许使用方法”警告,而不是找不到。我在哪里做错了什么?任何帮助将不胜感激。

编辑:

  $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
    $handler = $this->notFoundHandler; // handle using the default Slim page not found handler
    return $handler($req, $res);
});

我忘了这个。此映射的hides / exchanges方法不允许页面=>找不到处理程序。在我看来,这是基于应用程序的错误。抱歉,我的粗心...

3 个答案:

答案 0 :(得分:1)

我的建议是为您添加的群组添加root

$app->group('/admin', function(){};

在此之下,您应该添加

$this->get('', function(){/*-----activities----*/});

并在使用网上论坛时尝试使用 this

答案 1 :(得分:0)

$app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
    $handler = $this->notFoundHandler; // handle using the default Slim page not found handler
    return $handler($req, $res);
});

我忘了这个。此映射的hides / exchanges方法不允许页面=>找不到处理程序。在我看来,这是基于应用程序的错误。抱歉,我的粗心...

但是我认为notFoundHandler不是正确的选择。对于这种情况,必须引发自定义错误消息,例如405方法不允许-(notFoundHandler返回404)

答案 2 :(得分:0)

不确定是否是这种情况,但是如果您将路线分组,则需要将$ app-> get或$ app-> post更改为$ this-> get或$ this-> post。