【问题标题】:Route::get('(:any)',function() not working properly in laravel 4Route::get('(:any)',function() 在 laravel 4 中无法正常工作
【发布时间】:2014-05-07 08:53:43
【问题描述】:

该程序是一个 URL 缩短器,我只是从一些教程中尝试(对于 Laravel 3,但我使用的是 laravel 4),当我单击缩短的 URL 时,它应该给我“一些 URL”作为输出.

   <?php

Route::get('/', function()
{   
    return View::make ('home.index');
});

Route::post('/',function(){
    $url = Input::get('url');

    // If the url is already in the table, return it
    $record=Url::whereurl($url)->first();

    if ($record) {
        //then return it 
        return View::make('home.result')
                ->with('shortened', $record->shortened);
    }

});

Route::any('{shortened}',function($shortened)   
    { echo "Everything is ok with: ".$shortened; })
        ->where('shortened', '.*');

而是进入一个错误页面说

“未找到请求的 URL /asdf 在此服务器上未找到。”

我认为这是我的一个非常简单的错误。我不确定他们是否更改了语法或关键字。我也无法从 laravel 文档中找到任何解决方案。

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    我在我的 CMS 中使用以下内容通过 Regex 过滤器捕获所有请求:

    Route::get('{uri}', 'PublicController@init')
       ->where('uri', '.*');
    

    在你的情况下,它可能看起来像这样:

    Route::get('{uri}', function($uri) {
       return 'Hello, world!';
    })->where('uri', '.*');
    

    您可以使用第一个在控制器中加载方法。控制器为PublicController,方法为init。在那里,您可以处理您的错误并根据请求启动任何其他流程。

    这非常有效,您可以在该路由之前添加保留路由,使其成为捕获所有先前路由未捕获的请求的最后一个路由。

    【讨论】:

    • Route::get('{shortened}', 'PublicController@init') ->where('shortened', '.*'); Route::get('{shortened}', function() { return 'Some url'; })->where('shortened', '.*');尝试了这段代码,但仍然没有得到任何...我也不确定如何使用上面的代码..我是初学者...对不起...
    • 你说'试过这段代码但仍然没有得到任何',将$uri作为参数传递给函数参数,这样你就可以访问$uri。
    • @ArthurBALAGNE function($uri) { 在第二个示例中丢失,它应该像现在一样工作。
    • Route::get('{shortened}',function($shortened) { return'some url'; })->where('shortened', '.*');试过这个不起作用...
    • 你能用URL/index.php/asdf试试吗。如果可行,则未启用 mod_rewrite。
    【解决方案2】:

    在 Laravel 4 中,你可以这样做:

    Route::get('{shortened}', function($shortened)
    

    而不是

    Route::get('(:any)',function($shortened)
    

    您可以在此处阅读有关 Laravel 4 中路由参数的更多信息:http://laravel.com/docs/routing#route-parameters

    【讨论】:

    • 是的,刚刚尝试过...效果不佳...并且还浏览了文档,它似乎很模糊...
    • 是的,我第一次看到错字...并且也更改了它...它似乎对我不起作用...:(
    • 您确定,您在 Apache 服务器上启用了mod_rewrite
    • 是的已启用它...重新启动了 apache 服务器...清除了缓存,但什么都没有发生...
    • 你是对的...我没有启用 mod_rewrite...如果我们在 httpd.conf 文件中启用它就足够了...谢谢。
    【解决方案3】:
    1. 劳赫拉拉维尔

      转到您的应用程序根目录,运行命令 PHP artisan serve

    2. 放路由

      -在routing.php中注释evrything,添加这些路由:

      Route::get('/', function() { 回声“你好”; });

      Route::any('{shortened}',function($shortened){ echo "一切正常:".$shortened; })->where('shortened', '.*');

    3. 启动您的应用程序 -转到http://localhost:8000/asdf

    【讨论】:

    • 试过了...不行...都试过了:Route::get('(.*)',function($shortened) { return'some url'; });和; Route::any('(.*)',function($shortened) { return'some url'; });
    • 我的错,我以为你是 lavavel3 用户,我不知道为什么...这是 laravel4 放置正则表达式的方式:Route::get('page/{foo}' , function($foo) { return "$foo."; })->where('foo', '[A-Za-z0-9]+');
    • 我在本地主机上,所以代码是; Route::get('/{shortened}', function($shortened){return "Some url";})->where('shortened');是吗?
    • Route::get('{shortened}', function($shortened){return "Some url";})->where('shortened', '[A-Za-z0-9 ]+');不要以斜杠开头你的 URL,'where' 函数有 2 个参数,调整正则表达式来捕捉你想要的
    • 我需要 15 个声望才能投票...我在此之前尝试过投票...但无法...但是如果我达到 15 个声望肯定会回来投票...跨度>
    猜你喜欢
    • 2021-04-06
    • 2019-05-17
    • 2021-08-13
    • 2021-02-04
    • 2018-04-24
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    相关资源
    最近更新 更多