Php 我应该如何为Laravel5.2中的每个页面创建路线?
我是L5新手,这里是stackoverflow。 我正在制作一个基于存储库模式的博客服务。 现在我已经创建了一些服务类(如ImageService、PostService、CommentService…)和存储库作为数据访问对象 从现在开始,我将发表意见。首先,我想写“首页”。 需要注入2个服务类。“图像服务(AllUsersAges)”、“邮政服务(allUsersPosts)” 我在这里停下来……麻烦你,最好的办法是什么。 请让我知道我的想法是好的,还是有什么办法Php 我应该如何为Laravel5.2中的每个页面创建路线?,php,laravel-5,Php,Laravel 5,我是L5新手,这里是stackoverflow。 我正在制作一个基于存储库模式的博客服务。 现在我已经创建了一些服务类(如ImageService、PostService、CommentService…)和存储库作为数据访问对象 从现在开始,我将发表意见。首先,我想写“首页”。 需要注入2个服务类。“图像服务(AllUsersAges)”、“邮政服务(allUsersPosts)” 我在这里停下来……麻烦你,最好的办法是什么。 请让我知道我的想法是好的,还是有什么办法 1.Creating Co
1.Creating Controller
class IndexController extends Controller
{
public function top(ImageRepository $image, PostRepository $post)
{
$images= $image->allUsersImages();
$posts= $post->allUsersPosts();
return view('/index')
->with('images', $images)
->with('posts', $posts);
}
public function mypage(ImageRepository $image,
PostRepository $post, $id)
{
$images= $image->allUsersImagesById($id);
$posts= $post->allUsersPosts($id);
return view('/index')
->with('images', $images)
->with('posts', $posts);
}
.
.
.
.
prepare methods for each page
}
2. Creating routing
Route::get('/top', 'IndexController@top');
Route::get('/mypage', 'IndexController@mypage');
.
.
.
嗯我觉得你已经做的也不错
class IndexController extends Controller
{
public function top(ImageRepository $image, PostRepository $post)
{ }
}
当您编写类似的内容时,Laravel dependency Manager
将为您将ImageRepository
和PostRepository
注入函数top(){}
现在你的问题是有没有比这更好的方法
IMHO:
你不需要要求依赖经理为你做这项工作。
只需在这样的函数中使用ImageRepository
和PostRepository
class IndexController extends Controller
{
public function top()
{
$image_repository = new ImageRepository();
$post_repository = new PostRepository();
}
}
现在我理解的是,
ImageRepository
和PostRepository
是您的DAL(数据访问层)
还是您的ORM(模型)
的包装器,那么为什么要在模型周围使用包装器,为什么不直接使用模型,因为您没有在单页访问中共享存储库的方法。直接调用模型即可。这些都是在拉威尔安全和常见的做法。嗯我认为你已经做的也很好
class IndexController extends Controller
{
public function top(ImageRepository $image, PostRepository $post)
{ }
}
当您编写类似的内容时,Laravel dependency Manager
将为您将ImageRepository
和PostRepository
注入函数top(){}
现在你的问题是有没有比这更好的方法
IMHO:
你不需要要求依赖经理为你做这项工作。
只需在这样的函数中使用ImageRepository
和PostRepository
class IndexController extends Controller
{
public function top()
{
$image_repository = new ImageRepository();
$post_repository = new PostRepository();
}
}
现在我理解的是,
ImageRepository
和PostRepository
是您的DAL(数据访问层)
还是您的ORM(模型)
的包装器,那么为什么要在模型周围使用包装器,为什么不直接使用模型,因为您没有在单页访问中共享存储库的方法。直接调用模型即可。在laravel中,这些是安全和常见的做法。如果您的存储库在容器中注册,您可以将其提供给构造函数。将其存储在类属性上。用任何方法调用它。文档中的这段代码实际上向您展示了如何做到这一点。如果您的存储库是在容器中注册的,那么您可以将其提供给构造函数。将其存储在类属性上。用任何方法调用它。这个关于文档的片段真正向您展示了如何做到这一点