我正在 Laravel 中处理子域,供应商来了并开了一家商店。创建商店时会分配一个子域,他/她可以通过该子域访问商店。 它在 local_host 以及临时服务器上都运行良好(URL:https://spoon-jet.invo.zone),但是当我将其部署在生产服务器上时(URL:https://quickxi-food. invo.zone/)它给出错误,“未知的命名参数$store”。我正在处理 API 的子域。
临时服务器
当我使用子域点击登台服务器 URL 时,即 https://red-rock.spoon-jet.invo.zone/api/login,它没有给出错误。
生产服务器
当我使用子域点击登台服务器 URL 时,即 https://red-rock.quickxi-food.invo.zone/api/login,它给出了错误。
red-rock is a subdomain/store-name
两台服务器上的代码是相同的。
API.php
Route::group(['domain' => '{store}.'.config('app.domain'), 'middleware' => ['localization']], function () {
Route::post('login', 'ApiController\AuthController@login');
Route::post('registration', 'ApiController\AuthController@registration');
Route::post('foroget_passworod', 'ApiController\AuthController@forgotPassword');
Route::post('change_passworod', 'ApiController\AuthController@changePassword');
Route::post('verify_verificationcode_forget', 'ApiController\AuthController@verifyVerificationCodeForgetPassword');
Route::post('verify_verificationcode', 'ApiController\AuthController@verify1VerificationCode');
Route::post('device_token', 'ApiController\AuthController@deviceToken');
});
app.domain用于staging是:spoon-jet.invo.zone,对于生产是quickxi-food.invo.zone
登录功能:
public function login(Request $request) {
$validator = Validator::make($request->all(), [
'mobilenumber' => 'required',
'password' => 'required',
'category' => 'required',
]);
if ($validator->fails()) {
return response()->json(["code" => config('constants.VALIDATION_FAILED'), "errors" => $validator->errors()]);
}
try {
$mobileNumber = $request->mobilenumber;
$password = $request->password;
$category = $request->category;
$count = 0;
if ($category == "cashier") {
return response()->jsonResponse(config('constants.ERROR'), trans('api.cashierIsNotAllowedToLogin'), null);
}
if (Auth::attempt(['mobilenumber' => $mobileNumber, 'password' => $password, 'category' => $category])) {
$data['data'] = null;
$data['count'] = $count;
$user = Auth::user();
if ($user->status == 1) {
if ($user->isverified == 1) {
foreach ($user->tokens as $token) {
$token->revoke();
}
foreach ($user->tokens as $token) {
$token->delete();
}
$token = $user->createToken('SpoonJet')->accessToken;
$user->token = $token;
if ($user->category == "vendor"){
$count = vendorNotificationCount($user->id);
}elseif($user->category == "user"){
$count = userNotificationCount($user->id);
}
$data['data'] = $user;
$data['count'] = $count;
return response()->jsonResponse(config('constants.SUCCESS'), trans('api.loginSuccessfully'), $data);
} else {
////Verification///
$data['data'] = $user;
$code = rand(rand(999, 9999), rand(999, 9999));
$msg = str_replace(' ', '%20', 'نورتنا ! فضلا أدخل الرمز ') . "%20" . $code;
sendVerificationCode($user->mobilenumber, $msg, $user->id, $code);
$data['verificationCode'] = $code;
return response()->jsonResponse(config('constants.ACCOUNT_VERIFICATION_NEEDED'), trans('api.pleaseVerifyYourAccount'), $data);
}
} else {
return response()->jsonResponse(config('constants.ADMIN_CONFIRMATION_NEEDED'), trans('api.youNeedAdminConfirmation'), json_decode('{}'));
}
} else {
$count = User::where('mobilenumber', $mobileNumber)->count();
$data = json_decode('{}');
if ($count > 0) {
return response()->jsonResponse(config('constants.ERROR'), trans('api.invalidPassword'), $data);
} else {
return response()->jsonResponse(config('constants.ERROR'), trans('api.invalidMobileNumber'), $data);
}
}
} catch (QueryException $exception) {
return response()->jsonResponse(config('constants.SERVER_ERROR'), $exception->getMessage(), null);
}catch (Exception $exception) {
return response()->jsonResponse(config('constants.SERVER_ERROR'), $exception->getMessage(), null);
}
}
完整的错误日志:
Symfony\Component\Debug\Exception\FatalThrowableError: Unknown named parameter $store in file /var/www/quickxi-
food/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54
#0 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array()
#1 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction()
#2 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Route.php(219): Illuminate\Routing\ControllerDispatcher->dispatch()
#3 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Route.php(176): Illuminate\Routing\Route->runController()
#4 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Router.php(681): Illuminate\Routing\Route->run()
#5 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#6 /var/www/quickxi-food/app/Http/Middleware/Localization.php(25): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#7 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): App\Http\Middleware\Localization->handle()
#8 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#9 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Routing\Middleware\SubstituteBindings->handle()
#10 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#11 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Routing\Middleware\ThrottleRequests->handle()
#12 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#13 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Router.php(683): Illuminate\Pipeline\Pipeline->then()
#14 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): Illuminate\Routing\Router->runRouteWithinStack()
#15 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Router.php(624): Illuminate\Routing\Router->runRoute()
#16 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Routing/Router.php(613): Illuminate\Routing\Router->dispatchToRoute()
#17 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(170): Illuminate\Routing\Router->dispatch()
#18 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(130): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}()
#19 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#20 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()
#21 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#22 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle()
#23 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#24 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle()
#25 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(63): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#26 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle()
#27 /var/www/quickxi-food/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#28 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(171): Fideloper\Proxy\TrustProxies->handle()
#29 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(105): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#30 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(145): Illuminate\Pipeline\Pipeline->then()
#31 /var/www/quickxi-food/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter()
#32 /var/www/quickxi-food/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle()
#33 {main}
PS:如果您还有其他需要,请告诉我。预先感谢您。
我找到了解决方案,url/route 的参数与控制器功能不匹配。 查看登录功能:
之前
public function login(Request $request) {
$validator = Validator::make($request->all(), [
'mobilenumber' => 'required',
'password' => 'required',
'category' => 'required',
]);
if ($validator->fails()) {
return response()->json(["code" => config('constants.VALIDATION_FAILED'), "errors" => $validator->errors()]);
}
try {
$mobileNumber = $request->mobilenumber;
$password = $request->password;
$category = $request->category;
$count = 0;
if ($category == "cashier") {
return response()->jsonResponse(config('constants.ERROR'), trans('api.cashierIsNotAllowedToLogin'), null);
}
if (Auth::attempt(['mobilenumber' => $mobileNumber, 'password' => $password, 'category' => $category])) {
$data['data'] = null;
$data['count'] = $count;
$user = Auth::user();
if ($user->status == 1) {
if ($user->isverified == 1) {
foreach ($user->tokens as $token) {
$token->revoke();
}
foreach ($user->tokens as $token) {
$token->delete();
}
// dd($user->createToken('SpoonJet'));
$token = $user->createToken('SpoonJet')->accessToken;
$user->token = $token;
if ($user->category == "vendor"){
$count = vendorNotificationCount($user->id);
}elseif($user->category == "user"){
$count = userNotificationCount($user->id);
}
$data['data'] = $user;
$data['count'] = $count;
return response()->jsonResponse(config('constants.SUCCESS'), trans('api.loginSuccessfully'), $data);
} else {
////Verification///
$data['data'] = $user;
$code = rand(rand(999, 9999), rand(999, 9999));
$msg = str_replace(' ', '%20', 'نورتنا ! فضلا أدخل الرمز ') . "%20" . $code;
sendVerificationCode($user->mobilenumber, $msg, $user->id, $code);
$data['verificationCode'] = $code;
return response()->jsonResponse(config('constants.ACCOUNT_VERIFICATION_NEEDED'), trans('api.pleaseVerifyYourAccount'), $data);
}
} else {
return response()->jsonResponse(config('constants.ADMIN_CONFIRMATION_NEEDED'), trans('api.youNeedAdminConfirmation'), json_decode('{}'));
}
} else {
$count = User::where('mobilenumber', $mobileNumber)->count();
$data = json_decode('{}');
if ($count > 0) {
return response()->jsonResponse(config('constants.ERROR'), trans('api.invalidPassword'), $data);
} else {
return response()->jsonResponse(config('constants.ERROR'), trans('api.invalidMobileNumber'), $data);
}
}
} catch (QueryException $exception) {
return response()->jsonResponse(config('constants.SERVER_ERROR'), $exception->getMessage(), null);
}catch (Exception $exception) {
return response()->jsonResponse(config('constants.SERVER_ERROR'), $exception->getMessage(), null);
}
}
现在
public function login(Request $request, $store) {
$validator = Validator::make($request->all(), [
'mobilenumber' => 'required',
'password' => 'required',
'category' => 'required',
]);
if ($validator->fails()) {
return response()->json(["code" => config('constants.VALIDATION_FAILED'), "errors" => $validator->errors()]);
}
try {
$mobileNumber = $request->mobilenumber;
$password = $request->password;
$category = $request->category;
$count = 0;
if ($category == "cashier") {
return response()->jsonResponse(config('constants.ERROR'), trans('api.cashierIsNotAllowedToLogin'), null);
}
if (Auth::attempt(['mobilenumber' => $mobileNumber, 'password' => $password, 'category' => $category])) {
$data['data'] = null;
$data['count'] = $count;
$user = Auth::user();
if ($user->status == 1) {
if ($user->isverified == 1) {
foreach ($user->tokens as $token) {
$token->revoke();
}
foreach ($user->tokens as $token) {
$token->delete();
}
// dd($user->createToken('SpoonJet'));
$token = $user->createToken('SpoonJet')->accessToken;
$user->token = $token;
if ($user->category == "vendor"){
$count = vendorNotificationCount($user->id);
}elseif($user->category == "user"){
$count = userNotificationCount($user->id);
}
$data['data'] = $user;
$data['count'] = $count;
return response()->jsonResponse(config('constants.SUCCESS'), trans('api.loginSuccessfully'), $data);
} else {
////Verification///
$data['data'] = $user;
$code = rand(rand(999, 9999), rand(999, 9999));
$msg = str_replace(' ', '%20', 'نورتنا ! فضلا أدخل الرمز ') . "%20" . $code;
sendVerificationCode($user->mobilenumber, $msg, $user->id, $code);
$data['verificationCode'] = $code;
return response()->jsonResponse(config('constants.ACCOUNT_VERIFICATION_NEEDED'), trans('api.pleaseVerifyYourAccount'), $data);
}
} else {
return response()->jsonResponse(config('constants.ADMIN_CONFIRMATION_NEEDED'), trans('api.youNeedAdminConfirmation'), json_decode('{}'));
}
} else {
$count = User::where('mobilenumber', $mobileNumber)->count();
$data = json_decode('{}');
if ($count > 0) {
return response()->jsonResponse(config('constants.ERROR'), trans('api.invalidPassword'), $data);
} else {
return response()->jsonResponse(config('constants.ERROR'), trans('api.invalidMobileNumber'), $data);
}
}
} catch (QueryException $exception) {
return response()->jsonResponse(config('constants.SERVER_ERROR'), $exception->getMessage(), null);
}catch (Exception $exception) {
return response()->jsonResponse(config('constants.SERVER_ERROR'), $exception->getMessage(), null);
}
}
你可以看到我在登录中添加了 $store 参数,通过添加这个问题就解决了,我希望这对以后的访问者有帮助。