doutouhe5343 2016-05-10 11:30
浏览 32

将表单发布到'/'获得301

At the root of the project ('/') I have a dashboard kind of page that is routed to 'Patterns:: index'. This works fine for loading the page with a GET, but I also have a form on this page that I want to send a POST request to the same action.

config/routes.php

Router::connect('/', 'Patterns::index');

The form create line on the view looks like so:

<?= $this->form->create($filter, ['url' => ['Patterns::index'], 'method' => 'post']) ?>

Note: it has also looked like this, but it didn't work either:

<?= $this->form->create($filter) ?>

The action that is generated by this form is the correct path to the project '../client-name' (the root), with a method of POST. But when I hit submit it tries to send the request to this route and gets a 301 then redirects to '../client-name/' (note the trailing forward slash).

If I manually edit the action in the browser in the HTML to have the trailing forward slash the request goes through as expected - It sends a POST request to 'Patterns::index'.

Something else to note is that if I remove the line in config/routes and just access it by going to '/patterns' everything works as intended...

Can someone point me in the right direction?

  • 写回答

1条回答 默认 最新

  • douzi3756 2016-06-01 23:34
    关注

    It is probably your web server and not Lithium that is causing the 301 redirect. You may have a physical folder named client-name in the web root. Your web server will automatically add the slash then as it intends to serve the index inside that folder. Another possibility is that your web server (apache? or nginx?) has a rewrite rule somewhere that does a 301 redirect to any url that doesn't have a trailing slash.

    Lithium uses the PHP_SELF environment variable to determine the base folder in the lithium\action\Request::_base() method.

    The url for the form action is generated via the lithium et\http\Router::match() method. There is a line in the code that strips all trailing slashes: https://github.com/UnionOfRAD/lithium/blob/7251cc28/net/http/Router.php#L434

    So Lithium is definitely biased towards urls that don't have a trailing slash. That limitation could warrant filing an issue in the project's github repository. If you edit the Router.php file in the framework and add this after the line that trims the path, it would always include the trailing slash for url paths without a suffix (i.e. file extension):

    if (!$suffix && $path !== '/') {
        $path .= '/';
    }
    

    I'm not sure if there's another way. IIRC, when I encountered this problem in a project, I moved the physical folder to a different location so my web server wouldn't issue the 301 redirect.

    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据