Route :: get('(:any)',function()在laravel 4中无法正常工作

时间:2014-05-07 08:53:43

标签: php laravel laravel-4

该程序是一个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', '.*');

而是会出现错误页面

  

&#34;未找到在此服务器上找不到请求的URL / asdf。&#34;

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

3 个答案:

答案 0 :(得分:0)

在Laravel 4中,您可以:

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

而不是

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

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

答案 1 :(得分:0)

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

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

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

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

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

这非常有效,您可以在路由之前添加保留路由,这样它就成为捕获所有未被先前路由捕获的请求的最后路由。

答案 2 :(得分:-1)

  1. Lauch Laravel

    转到你的应用程序root,运行命令PHP artisan serve

  2. 放置路由

    在routing.php中创建evrything,添加以下路由:

    Route :: get('/',function() {     echo'hello'; });

    路线::任何( '{}缩短',函数($缩短){     echo“Evrything is ok with:”。$缩短;     }) - &gt; where('shortened','。*');

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

相关问题