使用if语句根据URL更改页面详细信息



我是PHP新手,但我想根据使用if语句选择的发票更改页面内容,但我尝试了许多方法,这些方法对我来说都不可行,因为我使用的是pdf导出包。我的目标是根据页面url显示不同的内容。这里是控制器

InvoiceController

<?php
namespace AppHttpControllers;
use LaravelDailyInvoicesInvoice;
use LaravelDailyInvoicesClassesBuyer;
use LaravelDailyInvoicesClassesInvoiceItem;
use AppModelsInvoice1;
class InvoiceController extends Controller
{

public function show()
{
if('http://localhost/Laravel/HotelManagement/admin/invoice/1') {      
$customer = new Buyer([
'name'          => 'Ahmed',
'custom_fields' => [
'Email' => 'test@example.com',
],
]);
$item = (new InvoiceItem())->title('Reservation 1')->pricePerUnit(245);
$invoice = Invoice::make()
->buyer($customer)
->series('10')
->sequence(10000)
->dateFormat('m/d/Y')
->currencySymbol('SR')
->discountByPercent(10)
->taxRate(15)
->addItem($item);
return $invoice->stream();
}
else if('localhost/Laravel/HotelManagement/admin/invoice/2'){
$customer = new Buyer([
'name'          => ' Abdullah',
'custom_fields' => [
'Email' => 'test@example.com',
],
]);
$item = (new InvoiceItem())->title('Reservation 2')->pricePerUnit(467);
$invoice = Invoice::make()
->buyer($customer)
->series('10')
->sequence(10000)
->dateFormat('m/d/Y')
->currencySymbol('SR')
->discountByPercent(10)
->taxRate(15)
->addItem($item);
return $invoice->stream();
}
}
}

路线
Route::get('admin/invoice/{id}/',[InvoiceController::class, 'show']);

您可以通过这种方式恢复URL参数

路线
Route::get('admin/invoice/{id}/',[InvoiceController::class, 'show']);

URL示例

https://yoursite.com/admin/invoice/1234/
class InvoiceController extends Controller
{ 
public function show(int $id)
{
dd($id) // outputs 1234
}
}

在这种情况下使用http://localhost/Laravel/HotelManagement/admin/invoice/1这样的端点是坏主意。您可以对捕获的发票ID使用request()。例如:

if(request()->get('id') == 1) {      
// Do you logic here 
} 
else if(request()->get('id') == 2) {      
// Do you logic here for invoice 2
} 

相关内容

  • 没有找到相关文章

最新更新