我想知道这两种模式中的哪一种会更好:
/photos/123/edit
或
/photos/edit/123
目前我正在使用第一个,当我尝试Whoops, looks like something went wrong.
而不是/photos/123/editxx
时,我会404 not found
。
我不确定在哪里寻找错误,这些是photos
路线:
Route::get('photos/randpics','PhotosController@randpics');
Route::get('photos/{id}/edit','PhotosController@getEdit')->middleware(['auth']);
Route::post('photos/{id}/edit','PhotosController@postEdit')->middleware(['auth']);
Route::post('photos/{id}/retag', ['as' => 'photos.retag', 'uses' => 'PhotosController@retag'])->middleware(['auth']);
Route::post('photos/{id}/delete','PhotosController@delete')->middleware(['auth']);
Route::get('photos/{id}/albums', 'PhotosController@getAlbums')->middleware(['auth']);
Route::post('photos/{id}/albums', 'PhotosController@postAlbums')->middleware(['auth']);
Route::get('photos/{id}/detail','PhotosController@detail');
Route::get('photos/{id}','PhotosController@single');
Route::post('photos/like', 'PhotosController@postLike')->middleware(['auth']);
Route::post('photos/unlike', 'PhotosController@postUnlike')->middleware(['auth']);
Route::get('photos','PhotosController@index');
当我输入/photos/123/a/a/a/
时,会出现同样的错误,抱怨主布局模板中存在未定义的变量。
public function getEdit(Request $request, $id = null)
{
$pic = Pic::findOrFail($id);
if(Gate::denies('edit-pic',$pic)) {
abort(403);
}
$pic->text = htmlspecialchars($pic->text);
$years = array_combine(range(date("Y"), 1960), range(date("Y"), 1960));
$location = $pic->location;
$tags = $pic->tags;
$tagsarray = $pic->tagNames();
$albums = $pic->albums;
$locations = array();
$getlocations = Location::orderBy('city')->get();
foreach($getlocations as $gl)
{
$locations[$gl->id] = $gl->city.' ('.$gl->country.')';
}
$cats = Cat::lists('cat','cat')->all();
return view('photos.edit',compact('pic','location','tags','tagsarray','albums','locations','cats','years'));
}
public function postEdit(Request $request, $id = null)
{
$pic = Pic::findOrFail($id);
if(Gate::denies('edit-pic',$pic)) {
abort(403);
}
$this->validate($request, [
'title' => 'max:200',
'text' => 'max:3000',
'location' => 'required|exists:locations,id',
'cat' => 'required|exists:cats,cat',
'jahrprod' => 'date_format:Y'
]);
$pic->title = trim($request->input('title'));
$pic->text = trim($request->input('text'));
$pic->jahrprod = ($request->input('jahrprod')) ? $request->input('jahrprod') : null;
$pic->location_id = $request->input('location');
$pic->cat = $request->input('cat');
$pic->save();
#mail
$maildata = array(
'msg' => 'picture '.$id.' ( '.$request->input('title').' ) was edited by '.Auth::user()->username
);
Mail::send(['text' => 'emails.empty'], $maildata, function($message){
$message->to('xx@yy.de')->from('xx@yy.de')->subject('picture edited');
});
return back()->with('success','photo information updated');
}
看起来,有两个以上的URL段,Controller.php无法正常工作。 /domain.xy/1/2
显示8
但/domain.xy/1/2/3
引发错误Undefined variable: photos_online
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Auth;
use Cache;
use DB;
use App\Pic;
use App\Upload;
use Carbon\Carbon;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
dd(8);
//.......
}
}
答案 0 :(得分:1)
如果您遵循laravel的资源控制器指南 https://laravel.com/docs/5.2/controllers#restful-resource-controllers
你应该选择第一个。
但要解决问题,请告诉我们你的路线和控制器功能处理它。
打开位于app / http / providers中的AppServiceProvider 并使用此
替换boot()函数public function boot()
{
//initilizw $photos_online
$photos_online = Cache::rememberForever('index_countpics', function()
{
return Pic::count();
});
#pics today
if (Cache::has('pics_today')){
$pics_today = Cache::get('pics_today');
}else{
$pics_today = Pic::whereRaw('DATE(created_at) = DATE(NOW())')->count();
Cache::put('pics_today', $pics_today, Carbon::tomorrow());
}
# count waiting uploads
$waiting_uploads = Cache::rememberForever('waiting_uploads', function()
{
return Upload::where('accepted',0)->where('infos',1)->count();
});
# user online
$client_ip = request()->ip();
$check = DB::table('useronline')->where('ip', $client_ip)->first();
$username = (Auth::guest()) ? null : Auth::user()->username;
$displayname = (Auth::guest()) ? null : Auth::user()->displayname;
if(is_null($check)){
DB::table('useronline')->insert(['ip' => $client_ip, 'datum' => DB::raw('NOW()'), 'username' => $username, 'displayname' => $displayname]);
}else{
DB::table('useronline')->where('ip', $client_ip)->update(['datum' => DB::raw('NOW()'), 'username' => $username, 'displayname' => $displayname]);
}
DB::delete('DELETE FROM useronline WHERE DATE_SUB(NOW(), INTERVAL 3 MINUTE) > datum');
$users_online = DB::table('useronline')->whereNotNull('username')->get();
$guests_online = DB::table('useronline')->count();
#unread messages
$unread_messages = 0;
if(Auth::check()){
$unread_messages = Auth::user()->newThreadsCount();
}
view()->share('photos_online',$photos_online);
view()->share('pics_today',$pics_today);
view()->share('waiting_uploads',$waiting_uploads);
view()->share('users_online',$users_online);
view()->share('guests_online',$guests_online);
view()->share('unread_messages',$unread_messages);
}