Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Implement route-model binding by default #1052

Open
Mosaab-Emam opened this issue Dec 19, 2022 · 0 comments · May be fixed by #1059
Open

[Feature] Implement route-model binding by default #1052

Mosaab-Emam opened this issue Dec 19, 2022 · 0 comments · May be fixed by #1059
Labels

Comments

@Mosaab-Emam
Copy link
Contributor

Mosaab-Emam commented Dec 19, 2022

Assuming laravel_generator.php:

return = [
  ...
  'repository_pattern' => false,
  'resources' => false,
  ...
]

And assuming we are generating a model named Post, here's what gets generated in PostController.php:

  // code

  public function show($id): JsonResponse
  {
      /** @var Post $post */
      $post = Post::find($id);

      if (empty($post)) {
          return $this->sendError(
              __('messages.not_found', ['model' => __('models/post.singular')])
          );
      }

      return $this->sendResponse(
          $post->toArray(),
          __('messages.retrieved', ['model' => __('models/post.singular')])
      );
  }

  // code

This does not implement route model binding, which is a sane default in any project with policies. For example, this pattern is required if the developer wishes to implement policies via middleware or via controller helpers.

My suggestion is to generate code similar to this:

  // code

  public function show(Post $post): JsonResponse
  {
      if (empty($post)) {
          return $this->sendError(
              __('messages.not_found', ['model' => __('models/post.singular')])
          );
      }

      return $this->sendResponse(
          $post->toArray(),
          __('messages.retrieved', ['model' => __('models/post.singular')])
      );
  }

  // code

This returns identical results, while helping implement policies out of the box.

The caveat is that it's not a sane default when using the repository pattern, so this should only apply when that option is set to false.

@Mosaab-Emam Mosaab-Emam changed the title [Feature Request] Implement route-model binding by default [Feature] Implement route-model binding by default Dec 21, 2022
@Mosaab-Emam Mosaab-Emam linked a pull request Jan 9, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants