我的api.php
文件夹中的Routes
看起来像这样:
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('login', 'Api\User\LoginController@login');
和web.php
如下:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/system/login', 'System\LoginController@index');
这是index
的{{1}}方法上的代码:
System\LoginController.php
基本上,我在同一个项目中使用api。项目文件夹位于class LoginController extends Controller
{
use Requestable;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$response = $this->post('api/login',$request->all());
$responseArray = json_decode($response->getBody(), true);
session()->put('access_token',$responseArray['data']['token']);
return redirect()->to('/system/dashboard');
//
}
的{{1}}内。因此,每当我打开并使用Apache服务器时,api都会发送数据。如果我使用htdocs
命令为项目提供服务,则每次尝试从api获取数据时,Server都不会抛出任何错误,而是挂断电话,即使经过很长时间也不会返回任何数据。我想这是服务器如何处理工匠服务的问题。请有些人帮助我解决这个问题?
答案 0 :(得分:2)
如果仔细观察,这就是它的工作原理。
当您在浏览器中点击 --Controller code Start
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String add() throws Exception {
addDTO();
return "demo";
}
protected void addDTO() {
try {
service.addDTO();
} catch (Exception e) {
log.error("addDTO", e);
}
}
--- Controller code End
--- Abstract Service with concrete method
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
public void addDTO() throws Exception {
try {
E entity = this.entityFromDTO();
this.dao.add(entity);
// Added For Transaction Test START
TMstPackage packType = getPackageEntity();
mstPackageDao.add(packType);
throw new Exception("Transaction RollBack test");
// Added For Transaction Test END
} catch (Exception ex) {
Log.error("addData");
throw ex;
}
}
--- Concrete Service
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
protected TMstContainer entityFromDTO() throws Exception {
TMstContainer container = null;
try {
Date date = Calendar.getInstance().getTime();
container = new TMstContainer();
container.setCntCode("TEST1");
container.setCntDescription("Test Desc");
container.setCntDtCreate(date);
container.setCntDtLupd(date);
container.setCntStatus('A');
container.setCntUidCreate("SYS");
container.setCntUidLupd("SYS");
} catch (Exception e) {
log.error(e);
}
return container;
}
(Web请求)时,它会触发php,该php会寻找恰好是/system/login
的正确路由。
然后,它在LoginController中运行索引函数。
现在,它在运行(API请求)中找到以下行
System\LoginController@index
因此,在同一行上,同一PHP实例(正在处理Web请求)尝试自行调用!并且Web请求仍未完成。请记住,PHP具有同步特性,直到当前行返回之前,它才移至下一行。
如果您注意到here,则PHP的内置服务器是单个单线程进程
Web服务器仅运行一个单线程进程,因此如果请求被阻止,PHP应用程序将停止。
它一次只能处理一个请求。因此,由于Web请求仍未完成,并且Web请求正在等待其刚刚进行的API调用的答案,因此该API请求只是(在队列中)等待处理。死锁是一切都被挂起的原因。对于像Apache服务器这样的生产服务器而言,这不会发生,因为Apache可以在需要时派生多个进程/线程并将它们委托给单独的php实例进行处理