定义 Laravel 的基本 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18868063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define base url for laravel
提问by Claire
I am new to laravel. My url for my only controller is
我是 Laravel 的新手。我唯一控制器的网址是
http://localhost/myapp/public/users
In the views for the form functions, I don't want to have to do an absolute path to the post function, e.g.
在表单函数的视图中,我不想对 post 函数做一个绝对路径,例如
<form method='post' action='/myapp/public/user/update-user'>
I want to just set the users controller as the base url path so I can say in my form
我只想将用户控制器设置为基本 url 路径,这样我就可以在我的表单中说
<form method='post' action='/update-user'>
Is this possible?
这可能吗?
回答by Santosh
Use URL::to() or simply use laravel form {{Form::open(array('url'=>'route_name'))}}
使用 URL::to() 或简单地使用 Laravel 表单 {{Form::open(array('url'=>'route_name'))}}
回答by devo
You can use URL::to() function.Laravel supports RESTful API or RESTful routing. So it is not a good idea to use routing like this.So use like, In app/routes.php
你可以使用 URL::to() 函数。Laravel 支持 RESTful API 或 RESTful 路由。所以像这样使用路由不是一个好主意。所以使用like,在app/routes.php
// for view (GET)
Route::get('users', function(){
View::make('users');
});
Route::post('users', function(){
// do form validation and process here or call a function from controller
});
Then in your form use,
然后在你的表单中使用,
echo Form::open();
// form elements
echo Form::close();
回答by Benubird
Yes, it is possible, but it's a little complicated because laravel isn't really set up to support this.
是的,这是可能的,但它有点复杂,因为 laravel 并没有真正设置为支持这一点。
You need to change the file vendor/laravel/framework/src/Illuminate/Http/Request.php
, replacing the function root()
with this:
您需要更改文件vendor/laravel/framework/src/Illuminate/Http/Request.php
,将函数替换为root()
:
public function root($domain = NULL)
{
return rtrim(($domain?:$this->getSchemeAndHttpHost()).$this->getBaseUrl(), '/');
}
And then in the getRootUrl($scheme, $root = null)
method in the file vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php
you need to change this line:
然后在getRootUrl($scheme, $root = null)
文件中的方法中,vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php
您需要更改这一行:
$root = $root ?: $this->request->root();
to this:
对此:
$root = $this->request->root($root);
That should allow laravel to automatically detect and handle subfolders correctly, so you will be able to use the standard URL methods.
这应该允许 laravel 自动检测和正确处理子文件夹,因此您将能够使用标准的 URL 方法。
回答by mhh1422
First, you need to hide the /public/ part of your URL using the virtual host or the alias in your web server (apache or ...). The entry point (http://some.host/alias/or http://some.virtual.host/) should lead directly to the public folder (i.e. /whatever/path/myapp/public/).
首先,您需要使用虚拟主机或 Web 服务器中的别名(apache 或 ...)隐藏 URL 的 /public/ 部分。入口点(http://some.host/alias/或http://some.virtual.host/)应直接通向公共文件夹(即 /whatever/path/myapp/public/)。
Second, using the routes, you can do the remaining easily. The route
其次,使用路线,您可以轻松完成其余的工作。路线
Route::get('update_users', "UsersController@updateAction")
will redirect the http://whatever.host/update_usersto the correct action without the need of the ugly format.
将http://whatever.host/update_users重定向到正确的操作,而不需要丑陋的格式。