Skip to content

u/slipperydippery

In this archive from Aug 2012 to Mar 2023

7
posts
50
comments
0
media
120
score
1 saved before deletion

Top posts

What do you guys think about treating pivot tables as a Models with their own Controllers?

I recently came across this video from the US Laracon 2017 with Adam Wathan on "Making your applications cruddy by design". His talk is about cleaning up your controllers with the guideline: "if it isn't part of the basic Restful methods of that controller, create a new controller." In the third sec...

10 points10 comments
Open on Reddit

What is the best controller structure for a model that is created over the course of more than one page? (without Vue)

I am in the process of simplifying a website, and one of the redesign concepts has been: "only ask one thing of the user per page". Users on our website create a Scan (but it could just as well be an Article, Item etc.) Instead of asking the user for all of the information on one page, we're now ask...

3 points9 comments
Open on Reddit

Top comments

I'm not going to read 100+ books in 2016. Even the author is only reading a maximum of 12 books this year, and has only "at least started a couple of chapters" in the "majority of the books".

I know it's fun to have a decked out design library. But I'd rather have a few really solid and personal recommendations than this random list of "classics".

...and three books by Stephen Hawking?

10 points

I don't think your problem is that web.php is too big at all. I think the issue is that your controller methods are overly complicated. If you create semantically better (and simpler!) controllers, your web.php will become semantically clearer and easier to manage.

Currently you have only 2 controllers: BlogController and CmsController. I understand the instinct to do this, but common practice (and the way Laravel is set up) is to have your Controllers control a Model. And since indexing/storing/creating Blog and Cms Model's don't make much sense (and you probably don't have those models in your project), I'd much rather see UsersController, PostsController, Pagesontroller, CommentsController, UserPostsController, etc. Here, indexing/storing/creating/etc Users, or Posts makes a lot of sense. Also, having the controllers freed up from the context of being either part of the CMS or part of the Blog will allow your controllers to be much more versatile (especially once you're working with API calls).

Try doing this: whenever you create a Controller, for example PostsController, create it with php artisan make:controller PostsController --resource, and try to stick to the provided methods only. Now, for your routes in web.php you can be explicit:

Route::get('/post', 'PostsController@index')->name('post.index');
Route::get('/post/{post}', 'PostsController@show')->name('post.show');
etc.

Or you could you could use

Route::resource('post', 'PostsController');

Which will automatically create all 7 routes corresponding with your 7 controller methods. This will greatly increase the readability and manageability of your web.php.

-- next steps --

Now, if you want to list all posts by a certain user, that won't fit in either PostsController or UsersController, but you can create a new controller: UserPostsController(as a resource), and now you have acces to:

Route::get('/user/{user}/post', 'UserPostsController@index')->name('user.post.index');
or 
Route::resource('user.post', 'UserPostsController'); 

And Route::resource here will automatically generate nested routes to your controller methods.

4 points

Subreddits

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.