我想要的只是使用一个控制器,它应该处理我的laravel 4应用程序的每个请求.问题是stackoverflow或其他地方的解决方案都没有对我有用.
这就是我现在拥有的:
Route::any('(.*)', function(){
return View::make('hello');
});
Run Code Online (Sandbox Code Playgroud)
现在浏览页面时我每次都会收到错误说:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Run Code Online (Sandbox Code Playgroud)
希望有人能帮助我!
我有以下问题,我需要配置Nginx,所以在任何URL用户访问时,它将保留uri(示例domain.com/some/url/),但只传递给laravel /并让Angular处理路由.
Route::get('/', function(){
return view('index');
});
Run Code Online (Sandbox Code Playgroud)
当访问/api/{anything}Laravel时会启动.
现在我index.html从公共文件夹返回,直到找到解决方案这是我的配置:
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
location /api {
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以做一条路线:
Route::get('{anything?}', function(){
return view('index');
});
Run Code Online (Sandbox Code Playgroud)
但是要广泛.
更新:
location / {
rewrite ^/(.*)$ / break;
index index.php;
try_files $uri $uri/ /index.php;
}
location /api {
index index.php;
try_files $uri $uri/ /index.php?$query_string;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个routes.php可以处理可选的无限子路径的路由。
Route::get('/path/{url}', function($url){
echo $url;
});
Run Code Online (Sandbox Code Playgroud)
url 可以是以下内容:
/path/part1
/path/part1/part2
/path/part1/part2/part3
etc.
Run Code Online (Sandbox Code Playgroud)
但由于/url 中的子路径不匹配,所以什么也没有发生。(echo $url当然,这只是为了测试)。
我现在使用一个技巧来避免这种情况,通过使用~而不是/子路径,然后替换它们,但我想知道是否有更好的方法,这样我就可以/在 URL 中使用。
更新
找到了解决方案,感谢马克:
Route::get('/path/{all}', function($url){
echo $url;
})->where('all', '.*');
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的Laravel 4 REST API中创建一个默认路由,当我的其他任何定义的路由都没有匹配请求时,该路由会将特定错误返回给调用者.
这可能吗?不幸的是,我在文档中找不到任何内容,所以我玩了一下并尝试使用通配符(*)作为我的最后一个路径定义,routes.php但这不起作用.
Route::when("*", function(){
throw new CustomizedException('Route not found...');
});
Run Code Online (Sandbox Code Playgroud)
当我有这条路线并做一个artisan routes,我得到一个例外:
{"error":{"type":"ErrorException","message":"Object of class Closure could not be converted to string","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Foundation\/Console\/RoutesCommand.php","line":153}}
调用不存在的路由不会触发我的自定义异常,而是标准的异常:
{"error":{"type":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"","file":"\/Applications\/MAMP\/htdocs\/CampaigningTools\/vendor\/laravel\/framework\/src\/Illuminate\/Routing\/Router.php","line":1429}}
我也试过any按照这篇文章的建议使用,但这也行不通:
Route::any( '(.*)', function( $page ){
throw new ValidationException('Custom error');
});
Run Code Online (Sandbox Code Playgroud)
当我呼叫一条不存在的路线时,这条路线也不会开火.
任何提示,我做错了将不胜感激.