Laravel middleware redirection -
when use laravel middleware routes not work properly
<?php namespace app\http\controllers; use auth; use app\article; use app\http\requests; use illuminate\http\request; use app\http\requests\articlerequest; use app\http\controllers\controller; use carbon\carbon; //use illuminate\http\request; class articlescontroller extends controller { public function __construct(){ $this->middleware('auth',['only'=>'create']); } // public function index(){ //return \auth::user(); $articles = article::latest('published_at')->published()->get(); return view('articles.index',compact('articles')); } public function show($id){ $article = article::findorfail($id); //dd($article->published_at->adddays(8)->diffforhumans()); return view('articles.show',compact('article')); } public function create(){ if(auth::guest()){ return redirect('articles'); } return view('articles.create'); } public function store(articlerequest $request){ /* $input = request::all(); $input['published_at'] = carbon::now(); */ $article = new article($request->all()); auth::user()->articles()->save($article); //article::create($request->all()); return redirect('articles'); } public function edit($id){ $article = article::findorfail($id); return view('articles.edit', compact('article')); } public function update($id, articlerequest $request){ $article = article::findorfail($id); $article->update($request->all()); return redirect('articles'); } }
when go http://localhost/lernlaravel/public/articles/create works fine when go http://localhost/learnlaravel/public/articles redirect http://localhost/articles.
index() method used listing articles how can fix it?
the redirect ()
accepts url path if want ensure redirect work on both testing , production environments, pass either action ()
or route ()
of applications redirect calls. in case go with
return redirect(action ('articlescontroller@show', $articles->id));
this way laravel automatically generate proper url path controller want handle request.
if choose go route()
required have named route in routes file, find resourceful controllers it's less complicated go action.
Comments
Post a Comment