我想重用我的代码,所以我不会重复自己。 我已经制作了PostController和Post模型。现在,我想创建一个管理员视图页面,在其中可以隐藏我的帖子。问题是这里如何将Postcontroller重用到我的管理员视图中?
要提到我是Laravel的新手
use Illuminate\Http\Request;
use App\Post;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::all();
return view('posts.post', compact('posts'));
} ...
namespace App\Http\Controllers;
use App\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\PostsController;
class AdminController extends PostsController
{
public function index()
{
$posts = Post::all();
return view('admin.index', compact('posts'));
}
}
答案 0 :(得分:0)
如果要对前端和管理员使用单个PostController,则必须检查当前登录的防护是 admin 或 user 来呈现视图:
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Auth;
class PostsController extends Controller
{
public function index()
{
$posts = Post::all();
@if(Auth::guard('admin')->check())
return view('admin.index', compact('posts'));
@elseif(Auth::guard('user')->check())
return view('posts.post', compact('posts'));
@endif
}
}