我需要下载主管发布的表格。我无法访问该页面,但无法访问该表单。它给了我ERR_INVALID_RESPONSE
错误,但主管可以轻松下载它。
中间件有问题吗?我挣扎了很多,但仍然无法下载,如果有人可能知道该问题,请帮助我。
控制器
class DutiesController extends Controller
{
public function assignSupervisor(Request $request, $id)
{
$assignS = new Duty;
$assignS->student_id = $id;
$assignS->user_id = $request->supervisor_id;
$assignS->student_name = $request->student_name;
$assignS->save();
return back();
}
public function assignInstructor(Request $request, $id)
{
$assignS = Duty::where('student_id', $id)->first();
$assignS->admin_id = $request->instructor_id;
$assignS->save();
return back();
}
public function duties($id)
{
$duty = Duty::where('student_id', $id)->orWhere('user_id', $id)->orWhere('admin_id', $id)->first();
return view('Duty.show', compact('duty'));
}
public function assign(Request $request, $id)
{
$assign = Duty::findOrfail($id);
if ($request->hasFile('duty')) {
$this->validate($request, [
'duty' => 'required|file|mimes:pdf,doc'
]);
$fileNameWithExt = $request->file('duty')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extension = $request->file('duty')->getClientOriginalExtension();
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
$path = $request->file('duty')->storeAs('public/duty', $fileNameToStore);
$assign->duty = $fileNameToStore;
}
$assign->marks = $request->marks;
$assign->save();
return back();
}
public function getduty($id) // my download function
{
$download = Duty::findOrfail($id);
return Storage::download("/public/duty/".$download->duty);
}
public function assignSupervisorInstructor()
{
$users = User::with('campus')->where('role_id', 4)->get();
$supervisors = User::with('campus')->where('role_id', 2)->get();
$instructors = Admin::where('role_id', 3)->get();
return view('Assigning.index', compact('users', 'supervisors', 'instructors'));
}
}
路线
Route::group(['middleware' => 'auth:web,admin'], function () {
//Now this routes can be accessible by both admin as well as
Route::get('/duties/downloads/{id}', 'DutiesController@getduty');
Route::post('/duties/assign/{id}', 'DutiesController@assign');
Route::get('/duties/myduties/{id}', 'DutiesController@duties');
Route::get('/duties/mydutty/{id}', 'DuttyController@duties');
Route::post('/duties/{id}', 'DutiesController@assignSupervisor');
Route::get('/assign', 'DutiesController@assignSupervisorInstructor');
Route::post('/duties/inst/{id}', 'DutiesController@assignInstructor');
});
刀片
<td>
<a href="/duties/downloads/{{$duty->id}}">
<button class="btn btn-success"><i class="fa fa-download"></i> Dowload Document</button>
</a>
</td>
答案 0 :(得分:0)
我希望这就是您的意思,但这就是我通过中间件区分用户类型的方式。 基本上是创建自定义中间件(我想您也可以通过内置功能来做到这一点,但是我更喜欢这样做),例如:
1)中间件:
app/Http/Middleware/CheckUserType.php
:
namespace App\Http\Middleware;
use Auth;
use Closure;
use App\Usergroup;
class CheckUserType
{
/**
* This middleware checks if the user is logged in as a specific type
*
* @var array
*/
public function handle($request, Closure $next, ...$userGroups)
{
if (in_array(UserGroup::where('id', Auth::user()->groupid)->first()->title, $userGroups)) {
return $next($request);
} else {
return response('Unauthorized...', 401);
}
}
}
在我的情况下,我有一个usergroups表链接到user-> groupid,因此我使用该模型将ID交叉引用到我在路由器中输入的组标题。
但是您可以明显地修改它。
另外请注意,我这样做是...$userGroups
,因此如果要在路由器中,可以迭代多种用户类型(请参见下文)。
2)在您的内核中注册:
然后在app/Http/Kernel.php
中注册:
添加到protected $routeMiddleware
中:
checkUserType' => CheckUserType::class
确保包括自定义中间件(use App\Http\Middleware\CheckUserType;
)
3)路线: 因此,最后在我的路由器中,例如,有以下内容:
/**
* @section Routes that require the user to be a Manager or an Administrator
*/
Route::group(['middleware' => 'checkUserType:Managers,Administrators'], function () {
//
});
或:
/**
* @section Routes that require the user to be an Administrator
*/
Route::group(['middleware' => 'check.usertype:Administrators'], function () {
// User Routes
Route::delete('/users/delete/{id}', 'UserController@deleteUser');
});