我想知道这两种模式中哪一种会更好:
/photos/123/edit
或者
/photos/edit/123
目前我正在使用第一个,Whoops, looks like something went wrong.
当我尝试时它给了我 a/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');
getEdit() 和 postEdit():
/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');
}
控制器.php
看起来,如果有超过 2 个 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);
//.......
}
}