[英]Laravel 5 Calling a class within a controller
Pre-info: Laravel 5 is my first framework I've used besides our custom framework that we've created over the years. 先决条件:Laravel 5是我多年来使用的自定义框架之外的第一个框架。 I'm still wrapping my head around the concepts but its mostly all there.
我仍在围绕这些概念,但大部分都在那里。 I have page calls, authorization checks, form submission and db queries all working.
我可以进行页面调用,授权检查,表单提交和数据库查询。
The issue: In the past I would create a new class "Access" and I'd call the function desired wherever I'd need to: 问题:过去,我将创建一个新类“ Access”,并在需要的地方调用所需的函数:
$access = Access::getAccessByAccount($accountID);
My hope is to do the same in laravel somehow and be able to call this public function from within a controller.. I just don't know how to call it and where to actually store the function. 我希望以某种方式在laravel中执行相同的操作,并能够从控制器内调用此公共函数。我只是不知道如何调用它以及在哪里实际存储该函数。
Here is a sample of the function I'd like to call: 这是我要调用的函数的示例:
public function getAccessByAccount($accountID){
//Grab all access rights set to given account ID
$accessList = DB::table('element_access')
->join('element', 'element.id', '=', 'element_access.element_id')
->select('element.name as element', 'element_access.permission as permission')
->where('element_access.account_id', $accountID)
->get();
//Return $access[element] = permission list or false if no access rights are assigned to account ID
if(is_array($accessList)){
$access = array();
foreach($accessList as $item){
$access[$item->element] = $item->permission;
}
return $access;
}else{
return false;
}
}
Here is how I'd like to somehow be able to call it in a controller: 这是我想以某种方式能够在控制器中调用的方式:
<?php namespace App\Http\Controllers\Portal\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller {
public function showAdminDashboard(){
$access = Access::getAccessByAccount(Auth::id());
if($access['admin-dashboard'] == 'r'){
return view('portal.admin.dashboard');
}
}
}
EDITS: 编辑:
Here is the solution I came up with with the help of the checked solution. 这是我在已检查的解决方案的帮助下想到的解决方案。
Created new file: app\\Library\\Access.php 创建新文件:app \\ Library \\ Access.php
<?php namespace App\Library;
use DB;
class Access{
public function getElementAccessByAccount($accountID){
//Grab all access rights set to given account ID
return DB::table('element_access')
->join('element', 'element.id', '=', 'element_access.element_id')
->select('element.name as element', 'element_access.permission as permission')
->where('element_access.account_id', $accountID)
->get();
}
}
To call the function: 调用函数:
$access = new \App\Library\Access;
$accessList = $access->getElementAccessByAccount(Auth::id());
If I was in your shoes, I would store all classes of custom functionalities to the app/services
directory. 如果我不知所措,我会将所有自定义功能类存储到
app/services
目录。
Access class 存取等级
<?php namespace App\Services;
class Access
{
public static function getAccessByAccount($accountID) {
//Grab all access rights set to given account ID
$accessList = \DB::table('element_access')
->join('element', 'element.id', '=', 'element_access.element_id')
->select('element.name as element', 'element_access.permission as permission')
->where('element_access.account_id', $accountID)
->get();
//Return $access[element] = permission list or false if no access rights are assigned to account ID
if (is_array($accessList)) {
$access = array();
foreach($accessList as $item){
$access[$item['element']] = $item['permission'];
}
return $access;
} else{
return false;
}
}
}
Controller 控制者
<?php namespace App\Http\Controllers\Portal\Admin;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function showAdminDashboard() {
$access = \App\Services\Access::getAccessByAccount(\Auth::id());
if($access['admin-dashboard'] == 'r') {
return view('portal.admin.dashboard');
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.