php - PHP 如果未登录,停止加载视图的正确方法
问题描述
我有一个 PHP 网站和一个登录页面。如果未创建会话“Caisse_login”,则在构造类时加载。我看到了一个我用 exit() 修复的错误,但我想知道是否有其他方法可以做到这一点。
这是我的课的开始:
类 caisses 扩展控制器{ 公共函数 __Construct(){ $this->caisseModel = $this->model('caisse'); if(!isset($_SESSION['Caisse_login']) 或 $_SESSION['Caisse_login']==FALSE){ $this->login(); 出口(); } }
每次输入 url 时,它都会调用相关的方法。所以如果我使用以下网址:
caisselive/en/caisses/showTable/3
因此,它将在 caisses 之上创建类,以及以下方法:
public function showTable($tableId = 0){
// control if there is at least one table
if(!$this->caisseModel->countTable()){
flash('Transaction', 'No any table have been created', 'alert alert-warning');
redirect('caisses/transaction');
}else if($tableId==0){
$data = [
'table' => $this->caisseModel->getAllTable(),
];
$this->view('caisses/tableShow', $data);
}else{
// control if the ID provided is an existing table
if(!$this->caisseModel->getTempTablebyID($tableId)){
flash('Transaction', 'The table does not exist', 'alert alert-warning');
redirect('caisses/showTable');
}else{
//Display the table
$totalPrice = 0;
$productsQuery = $this->caisseModel->getTempTableProducts($tableId);
$tempTable = $this->caisseModel->getTempTablebyID($tableId);
$i=0;
$k=0;
$totalPricePaid=0;
$productsPaid = null;
foreach($productsQuery as $products_1){
// control if it has already been paid
if($products_1->paid==0){
for($j=0;$j<$products_1->qty;$j++){
$products[$i]['productName'] = $products_1->product_name;
$products[$i]['productID'] = $products_1->id_product;
$products[$i]['transactionId'] = $products_1->id;
$products[$i]['rebate'] = $products_1->percentage;
$products[$i]['rebateID'] = $products_1->id_rebate;
$products[$i]['rebateName'] = $products_1->name;
$products[$i]['qty'] = 1;
$products[$i]['price'] = $products_1->price_s;
$products[$i]['priceTotInt'] = number_format($products_1->price_s * 1 * (1-($products_1->percentage/100)), 2);
$totalPrice = $totalPrice + $products[$i]['priceTotInt'] ;
$i++;
}
//create the list of what it has already have been paid
}else{
$productsPaid[$k]['productName'] = $products_1->product_name;
$productsPaid[$k]['productID'] = $products_1->id_product;
$productsPaid[$k]['transactionId'] = $products_1->id;
$productsPaid[$k]['rebate'] = $products_1->percentage;
$productsPaid[$k]['rebateID'] = $products_1->id_rebate;
$productsPaid[$k]['rebateName'] = $products_1->name;
$productsPaid[$k]['qty'] = $products_1->qty;
$productsPaid[$k]['price'] = $products_1->price_s;
$productsPaid[$k]['priceTotInt'] = number_format($products_1->price_s * $products_1->qty * (1-($products_1->percentage/100)), 2);
$totalPricePaid = $totalPricePaid + $productsPaid[$k]['priceTotInt'] ;
$k++;
}
}
$data = [
'products' => $products,
'productsPaid' => $productsPaid,
'dateTable' => $tempTable->date,
'nameTable' => $tempTable->nameTable,
'tableId' => $tableId,
'rebate' => $this->caisseModel->getAllRebate(),
'totalPrice' => number_format($totalPrice, 2),
'totalPricePaid' => number_format($totalPricePaid, 2)
];
$this->view('caisses/tableShowDetail', $data);
}
}
}
如果这个人没有登录会发生什么,它将加载 2 个视图,正如解释的那样,我在构建类时用 exit() 修复了它,但我想知道是否没有更好的方法呢?
解决方案
有不同的选择。如果您使用框架,则可以使用一种方法将此请求重定向到另一个控制器。另一个选项是设置一个布尔值来指示该用户未登录并且您在控制器中处理此参数。您已经实现的最简单的解决方案,只是一个退出,但是如果您需要在控制器中的每个操作结束时执行某些操作,这可能会导致一些问题。
推荐阅读
- python - Anaconda Navigator:无法管理频道
- typescript - 在 react-scripts 中导入 react-app-env.d.ts 会破坏 tsc 构建
- c# - 调整窗口大小时裁剪图像
- javascript - 在角度 8 中,如何将内容从一个组件拖放到另一个组件中
- java - 根据条件忽略 RequestBody 值
- netty - 关闭 Netty 中的通道并等待其处理程序被删除
- java - 如何使用消费者查找生产者的状态
- nsattributedstring - 为什么当我单击链接文本时,带有属性字符串的 NSTextField 会自动布局?
- shell - 在 Corda Enterprise v4.0 的 Corda Standalone Shell jar 中找不到主要清单属性
- if-statement - 谷歌表格中的排名