Php Laravel:控制器如何从路由访问参数?
很明显,我是拉雷维尔的不速之客,希望有人能帮助我 通过路由访问about屏幕Php Laravel:控制器如何从路由访问参数?,php,laravel,Php,Laravel,很明显,我是拉雷维尔的不速之客,希望有人能帮助我 通过路由访问about屏幕 Route::get('/about', array('as' => 'about', function() { return View::make('about')->with('title','About Screen')->with('class','about'); })); 变量$title和$class可以在about.blade.php中通过{{$title}}和{$cl
Route::get('/about', array('as' => 'about', function()
{
return View::make('about')->with('title','About Screen')->with('class','about');
}));
变量$title和$class可以在about.blade.php中通过{{$title}}和{$class}访问。如果中间有一个控制器
Route::get('hello/create', array('as' => 'create', 'uses' =>
'HelloController@create', function()
{
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}));
如何访问HelloController.php代码中的$title和$class(以便将值传播到后续视图)
另外,我确实知道/hello/create/{name of variable},这几乎是所有类似问题的答案,但我不知道如何使用它来传输未键入Http请求的变量。您正在阅读使用with()发送数据以查看。
使用with()中设置的$variablename在视图文件中回显它。示例:
您正在使用with()读取发送数据以查看。
使用with()中设置的$variablename在视图文件中对其进行回显示例:
$title
和$class
是手动提供给刀片的值。这些不是您在路由的GET参数中接收到的值。所以,你会像在结束时那样做
您的路线:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
控制器方法:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
更新:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
据我所知,您还可以在路由的闭包内调用控制器的方法,并将参数传递给控制器,并在控制器的方法内使用这些值调用视图
您的路线文件:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
控制器方法:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
$title
和$class
是手动提供给刀片的值。这些不是您在路由的GET参数中接收到的值。所以,你会像在结束时那样做
您的路线:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
控制器方法:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
更新:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
据我所知,您还可以在路由的闭包内调用控制器的方法,并将参数传递给控制器,并在控制器的方法内使用这些值调用视图
您的路线文件:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
控制器方法:
Route::get('hello/create', array('as' => 'create', 'uses' => 'HelloController@create'));
class HelloController{
public function create(){
return View::make('hello/create')->with('title','Create')->with('class','hello.create');
}
}
class HelloController{
public function create($title,$class){
return View::make('hello/create')->with('title',$title)->with('class',$class);
}
}
首先,你需要清理你的流程。目前,您正在手动设置要返回视图的变量,因此您的路线应如下所示:
Route::get('hello/create','HelloController@create');
然后,控制器处理逻辑:
公共函数创建(请求$Request)
{
返回视图('hello.create')->带有('title','create')->带有('class','hello.create');
}
现在,如果需要将参数从前端发送到控制器,有两个选项:
Route::get('hello/create/{a_variable}','HelloController@create');
然后您可以通过以下任一方式访问此参数:
公共函数创建(请求$Request)
{
返回视图('hello.create')->带有('a_变量',$request->a_变量);
}
或在方法中注入变量:
公共函数创建(请求$Request$a_变量)
{
返回视图('hello.create')->带有('a_变量',$a_变量);
}
选择2
对于查询参数的使用,在发出请求时应包括此选项。如果您的路线如下所示:
Route::get('hello/create','HelloController@create');
您可以像这样指定查询参数:
GET www.my-domain.com/hello/create?first_parameter=value_1&second_parameter=value_2
因此,在控制器中,您可以访问以下值:
公共函数创建(请求$Request)
{
$value_1=$request->get('first_参数');
$value_2=$request->get('second_参数');
返回视图('hello.create')
->带('value_1',$value_1)
->带('value_2',$value_2);
}
首先,您需要清除流程。目前,您正在手动设置要返回视图的变量,因此您的路线应如下所示:
Route::get('hello/create','HelloController@create');
然后,控制器处理逻辑:
公共函数创建(请求$Request)
{
返回视图('hello.create')->带有('title','create')->带有('class','hello.create');
}
现在,如果需要将参数从前端发送到控制器,有两个选项:
Route::get('hello/create/{a_variable}','HelloController@create');
然后您可以通过以下任一方式访问此参数:
公共函数创建(请求$Request)
{
返回视图('hello.create')->带有('a_变量',$request->a_变量);
}
或在方法中注入变量:
公共函数创建(请求$Request$a_变量)
{
返回视图('hello.create')->带有('a_变量',$a_变量);
}
选择2
对于查询参数的使用,在发出请求时应包括此选项。如果您的路线如下所示:
Route::get('hello/create','HelloController@create');
您可以像这样指定查询参数:
GET www.my-domain.com/hello/create?first_parameter=value_1&second_parameter=value_2
因此,在控制器中,您可以访问以下值:
公共函数创建(请求$Request)
{
$value_1=$request->get('first_参数');
$value_2=$request->get('second_参数');
返回视图('hello.create')
->带('value_1',$value_1)
->带('value_2',$value_2);
}
将GET路径中的值传递给co的唯一方法