1
因此,我是這個Laravel世界的新手,我遇到了一些問題。這裏我試圖創建一個鏈接來下載存儲在存儲/應用程序/記錄中的文件。但我不斷收到此異常: ExceptionImageLaravel錯誤:RouteCollection.php中的NotFoundHttpException第179行
這裏是我的刀片文件:這是一個重定向到控制器的鏈接應該是一部分,但我不知道如果我想傳遞變量的方式該控制器是正確的
<a href="{{ asset('admin/contracts/download/'.$contract->records) }}" class="btn btn-large pull-right"><i class="icon-download-alt"> </i> {{$contract->records}} </a>
這裏是我的路線文件(我只抄我認爲部分是值得一提的):
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function() {
Route::get('/home', '[email protected]');
Route::resource('contracts', 'Admin\ContractsController');
Route::get('contracts/download/{filename}', ['uses' => 'Admin\[email protected]', 'as' => 'contracts.downloadfile']);
});
而且我ContractsController(我複製了「商店」的方法,因爲我想證明在那裏我存儲我的文件(存儲/程序/記錄/),以及如何):
<?php namespace App\Http\Controllers\Admin;
use App\Contract;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreContractsRequest;
use App\Http\Requests\Admin\UpdateContractsRequest;
use Yajra\Datatables\Datatables;
use DB;
use Carbon\Carbon;
use JavaScript;
use Illuminate\Contracts\Logging\Log;
use Illuminate\Contracts\Filesystem;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
class ContractsController extends Controller
{
/**
* Display a listing of Contract.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (! Gate::allows('contract_access')) {
return abort(401);
}
$user = \Auth::user();
if (request()->ajax()) {
$query = Contract::query();
if (\Auth::getUser()->id==1 || \Auth::getUser()->id==2) {
$query->with("owner");
}
else {
$query->with("owner")->where('owner_id',$user->id)->get();
}
$template = 'actionsTemplate';
if(request('show_deleted') == 1) {
if (! Gate::allows('contract_delete')) {
return abort(401);
}
$query->onlyTrashed();
$template = 'restoreTemplate';
}
$table = Datatables::of($query);
$table->setRowAttr([
'data-entry-id' => '{{$id}}',
]);
$table->addColumn('massDelete', ' ');
$table->addColumn('actions', ' ');
$table->editColumn('actions', function ($row) use ($template) {
$gateKey = 'contract_';
$routeKey = 'admin.contracts';
return view($template, compact('row', 'gateKey', 'routeKey'));
});
$table->editColumn('contractsname', function ($row) {
return $row->contractsname ? $row->contractsname : '';
});
$table->editColumn('end_date', function ($row) {
return $row->end_date ? $row->end_date : '';
});
$table->editColumn('owner.name', function ($row) {
return $row->owner ? $row->owner->name : '';
});
return $table->make(true);
}
return view('admin.contracts.index');
}
/**
* Show the form for creating new Contract.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
if (! Gate::allows('contract_create')) {
return abort(401);
}
$owners = \App\User::select(
DB::raw("CONCAT(name,' ',lastname) AS name"),'id')
->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
return view('admin.contracts.create', compact('owners'));
}
/**
* Store a newly created Contract in storage.
*
* @param \App\Http\Requests\StoreContractsRequest $request
* @return \Illuminate\Http\Response
*/
public function store(StoreContractsRequest $request)
{
if (! Gate::allows('contract_create')) {
return abort(401);
}
if(!Storage::disk('local')->exists('Records')) Storage::makeDirectory('Records');
// var_dump($request->file('records')->getClientOriginalName());
$path=$request->file('records')->storeAs('Records',$request->contractsname.'_'.$request->l_name.''.Carbon::now()->format('Y-m-d-H-i-s').''.$request->file('records')->getClientOriginalName());
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
$contract = new Contract();
$contract->contractsname= $request->contractsname;
$contract->salutation= $request->salutation;
$contract->f_name= $request->f_name;
$contract->l_name=$request->l_name;
$contract->addresse=$request->addresse;
$contract->zihlerpunktnummer=$request->zihlerpunktnummer;
$contract->telephone=$request->telephone;
$contract->mobile=$request->mobile;
$contract->fax=$request->fax;
$contract->consumption_HT=$request->consumption_HT;
$contract->consumption_NT=$request->consumption_NT;
$contract->powersupplier=$request->powersupplier;
$contract->tension_MS=$request->tension_MS;
$contract->tension_HS=$request->tension_HS;
$contract->end_date=$request->end_date;
$contract->owner_id=$request->owner_id;
$contract->records=$path;
$contract->save();
return redirect()->route('admin.contracts.index');
}
/**
* Show the form for editing Contract.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
if (! Gate::allows('contract_edit')) {
return abort(401);
}
$owners = \App\User::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$contract = Contract::findOrFail($id);
return view('admin.contracts.edit', compact('contract', 'owners'));
}
/**
* Update Contract in storage.
*
* @param \App\Http\Requests\UpdateContractsRequest $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(UpdateContractsRequest $request, $id)
{
if (! Gate::allows('contract_edit')) {
return abort(401);
}
$contract = Contract::findOrFail($id);
$contract->update($request->all());
return redirect()->route('admin.contracts.index');
}
/**
* Display Contract.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
if (! Gate::allows('contract_view')) {
return abort(401);
}
$contract = Contract::findOrFail($id);
//show the csv File content
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
$file = fopen($storagePath.''.$contract->records, "r");
$file_contents = array();
if (($file = fopen($storagePath.''.$contract->records, "r")) !== FALSE) {
while (!feof($file)){
$line = fgetcsv($file,1000,';');
//$line = array_map("utf8_encode", $line);
array_push($file_contents, $line);
}
}
// echo "<pre>";
//$contract->records=Storage::url($contract->records);
//dd($contract->records);
// var_dump($file_contents);
// echo "</pre>";
fclose($file);
return view('admin.contracts.show', compact(['contract','file_contents']));
}
/**
* Remove Contract from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
if (! Gate::allows('contract_delete')) {
return abort(401);
}
$contract = Contract::findOrFail($id);
$contract->delete();
return redirect()->route('admin.contracts.index');
}
/**
* Delete all selected Contract at once.
*
* @param Request $request
*/
public function massDestroy(Request $request)
{
if (! Gate::allows('contract_delete')) {
return abort(401);
}
if ($request->input('ids')) {
$entries = Contract::whereIn('id', $request->input('ids'))->get();
foreach ($entries as $entry) {
$entry->delete();
}
}
}
/**
* Restore Contract from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function restore($id)
{
if (! Gate::allows('contract_delete')) {
return abort(401);
}
$contract = Contract::onlyTrashed()->findOrFail($id);
$contract->restore();
return redirect()->route('admin.contracts.index');
}
/**
* Permanently delete Contract from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function perma_del($id)
{
if (! Gate::allows('contract_delete')) {
return abort(401);
}
$contract = Contract::onlyTrashed()->findOrFail($id);
$contract->forceDelete();
return redirect()->route('admin.contracts.index');
}
private function _import_csv($path, $filename)
{
$csv = $path . $filename;
$query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE users FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 0 LINES (`firstname`, `lastname`, `username`, `gender`, `email`, `country`, `ethnicity`, `education` )", addslashes($csv));
return DB::connection()->getpdo()->exec($query);
}
public function downloadfile($filename)
{
return response()->download(storage_path('app/' . $filename));
}
}
日Thnx傢伙!
我改變對你說什麼,但路線例外仍然是相同的:/ –
你能夠訪問主頁,如果它然後張貼家庭網址 – iCoders
也確保你已經登錄,因爲你正在使用認證中間件 – iCoders