Source 代码 https://github.com/jslack2537/PHP-Calculator
我在运行我的计算器应用程序的业务逻辑时遇到问题,同时将结果保存到我的数据库中,它们独立工作,只是不同时。我不确定我是否应该组合我的两个控制器或这里的什么。关于组合它们或使流程按原样工作的最佳方式的任何建议。
<?php
namespace App\Http\Controllers;
use App\Result;
use Illuminate\Http\Request;
class ResultController extends Controller
{
public function postResultCreate(Request $request)
{ $result = 2;
$post = new Result([
'first_number' => $request->input('first'),
'second_number' => $request->input('second'),
'operator' => $request->input('operator'),
'sum' => $result
]);
$post->save();
return redirect('/')->with('info', 'Answer: ' .$result);
}
}
我的第二个控制器具有基本的数学逻辑
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CalculatorController extends Controller
{
public function calculatefunc(Request $request)
{
$operator = $request->input('operator');
$first_number = $request->input('first');
$second_number = $request->input('second');
$result = 0;
if ($operator == "plus")
{
$result = $first_number + $second_number;
}
elseif($operator == "minus")
{
$result = $first_number - $second_number;
}
elseif($operator == "multiply")
{
$result = $first_number * $second_number;
}
elseif($operator == "divide")
{
$result = $first_number / $second_number;
}
else{
$result= 0;
}
return redirect('/')->with('info', 'Answer: ' .$result);
}
}
慕容708150