Php 使用URL在控制器中调用函数
我来自CodeIgniter 如果您有这样一个控制器:Php 使用URL在控制器中调用函数,php,laravel,laravel-4,Php,Laravel,Laravel 4,我来自CodeIgniter 如果您有这样一个控制器: class Article extends CI_Controller{ public function comments() { echo 'Look at this!'; } } Route::get('/Article/comments}', 'ArticleController@comments'); class ArticleController extends BaseControl
class Article extends CI_Controller{
public function comments()
{
echo 'Look at this!';
}
}
Route::get('/Article/comments}', 'ArticleController@comments');
class ArticleController extends BaseController{
// GET: article/comments
public function getComments()
{
echo 'Look at this!';
}
// POST: article/comments
public function postComments()
{
// Do Something
}
}
您可以使用如下URL访问comments()
函数:example.com/Article/comments
我怎样才能在拉威尔做类似的事情?
我现在的做法是指定如下路线:
class Article extends CI_Controller{
public function comments()
{
echo 'Look at this!';
}
}
Route::get('/Article/comments}', 'ArticleController@comments');
class ArticleController extends BaseController{
// GET: article/comments
public function getComments()
{
echo 'Look at this!';
}
// POST: article/comments
public function postComments()
{
// Do Something
}
}
但我希望有一种更为动态的方式来实现这一点,因为我不想继续为每个函数创建新的路由对于Laravel用户,通过URL动态调用控制器方法的推荐方式是通过RESTful控制器:
<?php
class ArticleController extends controller {
public function getComment()
{
return 'This is only accesible via GET method';
}
public function postComment()
{
return 'This is only accesible via POST method';
}
}
那么如果你跟着
http://laravel.dev/articles/comments
使用浏览器,您应该会收到:
This is only accesible via GET method
命名控制器方法(getComment、postComment、deleteComment…)的方式告诉Laravel,应该使用HTTP方法来调用这些方法
检查文档:
但您也可以使用PHP将其动态化:
class ArticlesController extends Controller {
public function comments()
{
return 'Look at this!';
}
public function execute($method)
{
return $this->{$method}();
}
}
使用如下控制器:
Route::get('Article/{method}', 'ArticleController@execute');
那你只需要
http://laravel.dev/Article/comments
对于Laravel用户,建议通过URL动态调用控制器方法的方法是通过RESTful控制器:
<?php
class ArticleController extends controller {
public function getComment()
{
return 'This is only accesible via GET method';
}
public function postComment()
{
return 'This is only accesible via POST method';
}
}
那么如果你跟着
http://laravel.dev/articles/comments
使用浏览器,您应该会收到:
This is only accesible via GET method
命名控制器方法(getComment、postComment、deleteComment…)的方式告诉Laravel,应该使用HTTP方法来调用这些方法
检查文档:
但您也可以使用PHP将其动态化:
class ArticlesController extends Controller {
public function comments()
{
return 'Look at this!';
}
public function execute($method)
{
return $this->{$method}();
}
}
使用如下控制器:
Route::get('Article/{method}', 'ArticleController@execute');
那你只需要
http://laravel.dev/Article/comments
我建议您坚持使用laravel的方法来创建
REST控制器
,因为这样您就可以控制控制器方法调用的HTTP动词
。laravel实现这一点的方法只是在控制器方法前面添加HTTP动词
,对于您的方法comments
,如果您想在laravel中指定GET
请求,则方法的名称类似于getComments
例如,如果您需要对文章/评论
URI执行获取
请求,然后要创建一个新的评论,您希望将同一URI与另一个HTTP
动词一起使用,比如说发布
,您只需要执行以下操作:
class Article extends CI_Controller{
public function comments()
{
echo 'Look at this!';
}
}
Route::get('/Article/comments}', 'ArticleController@comments');
class ArticleController extends BaseController{
// GET: article/comments
public function getComments()
{
echo 'Look at this!';
}
// POST: article/comments
public function postComments()
{
// Do Something
}
}
进一步阅读:
现在,对于您的具体回答,这是您所要求的Laravel方法:
class ArticleController extends BaseController{
public function getComments()
{
echo 'Look at this!';
}
}
在routes.php
文件中,您需要添加控制器,如下所示:
Route::controller('articles', 'ArticleController');
我建议您坚持使用laravel的方法来创建
REST控制器
,因为这样您就可以控制控制器方法调用的HTTP动词
。laravel实现这一点的方法只是在控制器方法前面添加HTTP动词
,对于您的方法comments
,如果您想在laravel中指定GET
请求,则方法的名称类似于getComments
例如,如果您需要对文章/评论
URI执行获取
请求,然后要创建一个新的评论,您希望将同一URI与另一个HTTP
动词一起使用,比如说发布
,您只需要执行以下操作:
class Article extends CI_Controller{
public function comments()
{
echo 'Look at this!';
}
}
Route::get('/Article/comments}', 'ArticleController@comments');
class ArticleController extends BaseController{
// GET: article/comments
public function getComments()
{
echo 'Look at this!';
}
// POST: article/comments
public function postComments()
{
// Do Something
}
}
进一步阅读:
现在,对于您的具体回答,这是您所要求的Laravel方法:
class ArticleController extends BaseController{
public function getComments()
{
echo 'Look at this!';
}
}
在routes.php
文件中,您需要添加控制器,如下所示:
Route::controller('articles', 'ArticleController');
太棒了,我不知道你能用php做到这一点!在“$this->{$method}()”之前添加
return
;要返回视图吗?是的,当然,我只是忘了添加它。:)李先生。里贝罗,Route::controller('article','ArticleController')
不会实现他想要的吗?是的,会的。刚刚编辑,以支持一个纯粹的拉威尔方式这样做。谢谢。太棒了,我不知道你能用php做到这一点!在“$this->{$method}()”之前添加return
;要返回视图吗?是的,当然,我只是忘了添加它。:)李先生。里贝罗,Route::controller('article','ArticleController')
不会实现他想要的吗?是的,会的。刚刚编辑,以支持一个纯粹的拉威尔方式这样做。谢谢,你说得对。我只是颠倒了答案的顺序(它仍然在那里),将RESTful控制器设置为推荐的方式。感谢您的洞察力!我会坚持这个方法,你是对的。我只是颠倒了答案的顺序(它仍然在那里),将RESTful控制器设置为推荐的方式。感谢您的洞察力!我将坚持这种方法