← prev | next →
2021-01-06 17:30:32
If I want to create an API, is creating new API Routes necessary?
What do you think about creating it using if (request()->ajax()) in the web.php routes?! Will there be any problems? I think it may conflict with search engines. What do you think?
By the way, these routes/APIs don’t need any authentication.
2021-01-06 17:36:03
2021-01-06 17:30:32
If I want to create an API, is creating new API Routes necessary?
What do you think about creating it using if (request()->ajax()) in the web.php routes?! Will there be any problems? I think it may conflict with search engines. What do you think?
By the way, these routes/APIs don’t need any authentication.
It’s so frustrating when you write the same code twice
DesolatorMagno 2021-01-06 17:36:18
In reality you can do anything you wish, but having routes and controllers for api and web separates help a lot, because
– Middleware are not the same for them
– Testing is not the same.
– Authentication is not the same.
– The intended use is not the same.
– A api don’t have a front that have clues about it, you need to make a better documentation so users and developers know well what it expect, what it does and what expect as a return.
2021-01-06 17:38:31
DesolatorMagno 2021-01-06 17:36:18
In reality you can do anything you wish, but having routes and controllers for api and web separates help a lot, because
– Middleware are not the same for them
– Testing is not the same.
– Authentication is not the same.
– The intended use is not the same.
– A api don’t have a front that have clues about it, you need to make a better documentation so users and developers know well what it expect, what it does and what expect as a return.
Right… As I said the main problem is repeating the code in both API and WEB controllers
2021-01-06 17:39:37
I’ve made everything OOP, but anyways I’ve got to make an object at least in both of them
2021-01-06 17:40:53
2021-01-06 17:30:32
If I want to create an API, is creating new API Routes necessary?
What do you think about creating it using if (request()->ajax()) in the web.php routes?! Will there be any problems? I think it may conflict with search engines. What do you think?
By the way, these routes/APIs don’t need any authentication.
….
If (request()->ajax()) Resource…
else view…
Is so neat 😀
DesolatorMagno 2021-01-06 17:44:32
2021-01-06 17:30:32
If I want to create an API, is creating new API Routes necessary?
What do you think about creating it using if (request()->ajax()) in the web.php routes?! Will there be any problems? I think it may conflict with search engines. What do you think?
By the way, these routes/APIs don’t need any authentication.
The same can be said about MVC, you can do all the logic inside a single file.
DesolatorMagno 2021-01-06 17:45:24
you can always extract the logic inside a service..
DesolatorMagno 2021-01-06 17:46:28
Remember that even errors should be treated and returned differently.
DesolatorMagno 2021-01-06 17:49:02
There is like 100 pro of using separates files, and 1 con (repeating code) that can be fix with something like a service.
DesolatorMagno 2021-01-06 17:51:12
AS a bonus i will tell you, that i am working in a system where they put together api and web routes in the same controller and web, and some routes are mixed, so i have to navigate in the system looking for where the info came, jumping from controller to controller, so more often that not i remember the family of those that choice to do it in that way.
2021-01-06 17:52:27
DesolatorMagno 2021-01-06 17:46:28
Remember that even errors should be treated and returned differently.
You’re right, and of course at the end of the day I’ll need the user to be authenticated.
It it seems it worth to repeat the code in 2 separate classes.
Besides, api prefixe will be removed if I write everything in a single method.
hypernxf 2021-01-06 17:52:30
Completely agree. I’m a guilty party that mix my api and web controller in one object
2021-01-06 17:57:16
Siberfx 2020-10-01 15:28:53
show us the whole :)).. and change the delay for next message to 5 seconds please 😛
The minimum delay is 10sec
natghi2010 2021-01-06 20:10:01
hypernxf 2021-01-06 17:52:30
Completely agree. I’m a guilty party that mix my api and web controller in one object
You monster
hypernxf 2021-01-06 20:10:42
LoL to be fair my app was an upgrade from Laravel 5 🙂
hypernxf 2021-01-06 20:11:27
So api.php dont exists then haha. But even so, there’s still a slight different in putting routes in api.php in terms of authentication. So i still choose to put it in web.php
My only regret is I should have separate the Controllers
2021-01-06 20:17:02
hypernxf 2021-01-06 20:11:27
So api.php dont exists then haha. But even so, there’s still a slight different in putting routes in api.php in terms of authentication. So i still choose to put it in web.php
My only regret is I should have separate the Controllers
I have more than 10 controllers for my web.php routes. Even thinking of making the same thing for API scares me.
DesolatorMagno 2021-01-06 20:17:52
You are easy to scare…
hypernxf 2021-01-06 20:18:36
I have so many routes that i kinda split them up into multiple files lol
DesolatorMagno 2021-01-06 20:18:42
i have to work in a controller with a single method of 500 lines, because they wanted to do the job of several controllers in the same place, it have even internal function.
hypernxf 2021-01-06 20:19:03
wow.
hypernxf 2021-01-06 20:19:18
Ok as bad as my code was I dont think I can beat that haha
DesolatorMagno 2021-01-06 20:19:27
function update(){
function user(){…}
function company(){…}
}
DesolatorMagno 2021-01-06 20:19:50
but if you want to beat it, you are in the correct way 😂
hypernxf 2021-01-06 20:20:51
No thanks haha
hypernxf 2021-01-06 20:21:03
That’s too painful even for me to read….
DesolatorMagno 2021-01-06 20:26:05
hypernxf 2021-01-06 20:18:36
I have so many routes that i kinda split them up into multiple files lol
Yes, i also do that sometimes, i triy to do things like userRoute.php and group them i a logic way.
DesolatorMagno 2021-01-06 20:28:04
2021-01-06 20:17:02
I have more than 10 controllers for my web.php routes. Even thinking of making the same thing for API scares me.
If there are routes that don’t follow the restfull pattern, or if they are to big in logic, i create a new controller, i rather have a
UserFavoriteController
UserFollowController
UserHistoriesController
than a UserController with many unrelated methods.
DesolatorMagno 2021-01-06 20:28:44
I try that everything inside the controller follow rest standar method (edit,update,store,…)
DesolatorMagno 2021-01-06 20:30:41
You can also do things like
ControllerUsers
And inside of it put all Users related controllers, you don’t need to put all inside the same controller.
hypernxf 2021-01-06 20:30:52
DesolatorMagno 2021-01-06 20:28:44
I try that everything inside the controller follow rest standar method (edit,update,store,…)
yeah I do that as much as I can too. But sometimes I realize it’s just hard to follow. when it’s not meant to be
DesolatorMagno 2021-01-06 20:32:16
I find it harder to do the inverse, just putting userFollowUser() inside the UserController, seems ugly and harder for me to follow.
hypernxf 2021-01-06 20:33:02
I’ll just create a FollowUserController
hypernxf 2021-01-06 20:33:44
then use store and destroy for follow and unfollow that’s it
← prev | next →