gpt4 book ai didi

php - 拉拉维尔 4 : Can I tell which route is calling the controller function?

转载 作者:行者123 更新时间:2023-12-02 21:21:59 25 4
gpt4 key购买 nike

我有一个 Laravel 应用程序,有 2 组路线:

Route::group(array('prefix' => 'api'), function()
{
Route::post('/login', ['uses' => 'AuthenticationController@apiLogin']);
}

Route::group(array('prefix' => 'admin'), function()
{
Route::post('/login', ['uses' => 'AuthenticationController@adminLogin']);
}

为了节省时间并防止编写重复代码,我想压缩 apiLogin()adminLogin()函数到一个函数:login() 。我还想根据调用该函数的路线返回不同的内容。

如果请求来自/api/login我要回Response::json($apiResponse) 。如果请求到来 /admin/login我要回Redirect::('route.to.redirect.to')

有没有办法可以知道 Controller 函数是从哪里调用的? (最好不解析 URL)

最佳答案

您可以在 Controller 中检查路线。 (我并不是说这是解决您问题的最佳解决方案,但您可以)

最好的方法是命名您的路线。 Laravel Docs

Route::post('/login', ['as' => 'apiLogin', 'uses' => 'AuthenticationController@apiLogin']);

Route::post('/login', ['as' => 'adminLogin', 'uses' => 'AuthenticationController@adminLogin']);

然后你就这么做了

Route::currentRouteName();

如果由于某种原因您无法命名路线,您仍然可以获得路线的路径(这不是完整的网址,而是 route 定义的分段。包括组)

因此 Route::getCurrentRoute()->getPath() 应该返回 api/loginadmin/login

关于php - 拉拉维尔 4 : Can I tell which route is calling the controller function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27034406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com