How can I use "where" in a Laravel controller - php

If I have the next route:
Route::get('/user/{id}', function($id) {
return View::make(users.profile, array('id' => $id));
})->where(array('id' => '[0-9]+'));`
How could I do the same in a Restful controller?
Route::controller('/user', 'UserController');
My UserController:
class UserController extends BaseController {
public function getProfile($id) {
return View::make('users.profile', array('id' => $id));
}
}
Thanks for your attention.

where doesn't appear to work when chained onto a Route::controller call, but you can achieve the same functionality with the Route::pattern declaration. So, for example, this code for a Route::controller (called "implicit routing") would work, limiting id to numeric:
Route::pattern('id', '\d+');
Route::controller('/user/{id}', 'UserController');
Then, in UserController, the getIndex method would be called from a GET request:
class UserController extends BaseController {
public function getIndex($id) {
return View::make('users.profile', array('id' => $id));
}
}
Note, however, that this only works for the index method, i.e. for calls to http:://example.com/user/99. If you want to use other controller methods using "implicit routing", for example http:://example.com/user/profile/99 and the controller method getProfile($id), you need to declare your route without the {id} parameter, like so:
Route::controller('/user', 'UserController');
...in which case, you aren't able to use ->where or Route::pattern to constrain the {id}, since there is no {id} parameter to constrain.
In the end, you're better off going with "explicit routing," as you do at the beginning of your answer, or using RESTful Resource Controllers (see the docs) and specifying your route as:
Route::resource('user', 'UserController');.
If you subscribe to Laracasts, Jeffrey Way has a great, clear tutorial about some of the perils of "implicit routing" here.

To make sure about the methods and URL run the php artisan routes from your terminal so you'll get the list of all routes you have access to with their URL. In this case for the following route and controller you may find a URL like user/profile/10
// Route
Route::controller('/user', 'UserController');
// Controller
class UserController extends BaseController {
public function getProfile($id) {
return View::make('users.profile', array('id' => $id));
}
}
So use http://domain.com/user/profile/10, here 10 will be passed to the $id variable in your profile method. Also remember that, in RESTfull controller each method should be prefixed with the HTTP verb they responds to so in this case this method will respond to a GET request.

In order to do it you want to wrap your Route::controller statements in a group and apply where pattern for the group, since setting it globally might not be accurate for other routes:
Route::group('where' => ['id' => '\d+'], function () {
Route::controller('users', 'UsersController');
// other restful controller definitions with this pattern go here
}

Related

Declare same route twice but expect different behaviour according to a middleware

I started creating a REST API using the lumen framework and wanted to set up a particular behaviour for my GET /user route. Behaviour is the following:
If the request come from an authenticated user (using auth middleware), the method getAllFields from UserController is called and return all the data from the user
If it's not the case, the method get from UserController is called and return some of the data from the user
It seems logic to me to just write it like that in my web.php using a simple middleware:
<?php
$router->group(['middleware' => 'auth'], function () use ($router) {
$router->get('/user/{id}', [
'uses' => 'UserController#getAllFields'
]);
});
$router->get('/user/{id}', [
'uses' => 'UserController#get'
]);
But for some reason, even if the middleware is correct, I always get the response of the second route declaration (that call get()). I precise that if I remove the second route declaration, the one in the middleware work as expected.
Have someone an idea how I can achieve something similar that work?
Router will check if your request matches to any declared route. Middleware will run AFTER that match, so You cannot just return to router and try to find another match.
To fallow Laravel and Routes pattern - You should have single route that will point to method inside controller. Then inside that You can check if user is logged or not and execute getAllFields() from that controller. It will be not much to rewrite since You are currently using UserController in both routes anyway.
web.php
$router->get('/user/{id}', 'UserController#get');
UserController.php
public function get()
{
return auth()->check() ? YourMethodForLogged() : YourMethodForNotLogged();
}
Or if there is not much logic You can keep this in single method.
Also it is good idea to fallow Laravels REST standards (so use show instead of get, "users" instead of "user" etc - read more https://laravel.com/docs/7.x/controllers)
web.php
$router->get('/users/{user}', 'UserController#show');
UserController.php
public function show(User $user)
{
if (auth()->check()) {
//
} else {
//
}
}
To summary - for your needs use Auth inside controller instead of middleware.
To check if user is logged You can use Facade Auth::check() or helper auth()->check(), or opposite Auth::guest() or auth()->guest().
If you are actually using Lumen instead of full Laravel then there is not auth helper by default (You can make own or use package like lumen-helpers) or just keep it simple and use just Facades instead (if You have then enabled in Lumen).
Read more https://laravel.com/docs/7.x/authentication and https://lumen.laravel.com/docs/7.x/authentication
This pattern is against the idea of Laravel's routing. Each route should be defined once.
You can define your route without auth middleware enabled and then define your logic in the controller.

Laravel 5.5 Resource Controller and Dependecy Injection

I am working on a Laravel 5.5 application. When I use php artisan make:model SomeModel -mr it creates the model, migration and resource controller.
I've been noticed that some methods have by default only one parameter: the model:
public function show(SomeModel $someModel)
{
...
}
If you look into the $someModel variable it has an empty SomeModel object.
I was reading on Laravel Documentation that it looks like the Containers or Facades but I am not sure how to use this. Do you?
Edit 1:
I had my routes defined in routes/web.php as: Route::resource('users', 'UserController');
Now I had to define all the routes manually since automatic binding was not working:
Route::get('users', 'UserController#index');
Route::get('users/create', 'UserController#create');
Route::post('users', 'UserController#store');
Route::get('users/{user}/edit', 'UserController#edit', function(App\User $user) {});
Route::post('users/{user}', 'UserController#update', function(App\User $user) {});
Route::post('users/{user}/delete', 'UserController#destroy', function(App\User $user) {});
So, should I replace every resource controller route to manual routing like this?
The resource controller is expecting you to use route model binding. In your routes file, each route that corresponds to a controller action with an injected model will need to have a matching parameter.
For example:
Route::get('user/{user}', 'UserController#show');
Using the above route, the following controller action would receive a user instances that corresponds to the user ID passed as a URL parameter.
class UserController extends Controller
{
public function show(User $user)
{
...
}
}
The reason you're seeing an empty model now is that Laravel will just pass and fresh model to the controller if it is not bound to a route parameter. In other words, if you forget to bind the model in your routes file automatic injection will just give you a new instance.
Note that if you are using a route resource the resulting routes should already have the correct parameters
Route::resource('users', 'UserController');
You can run php artisan route:list to confirm that your actual routes are correct.
Your problem is your controller is expecting two parameters like below:
public function show($id, User $user)
if you try:
public function show(User $user)
it should work correctly.
In your route you are passing only a single param like:
user/{user}
So if you dd the first param it will display the number 1 but if you pass that
to the model it will return the corresponding user as per what id you pass in the route.
The reason your User model was returning an empty object was because there was no value passed to it.
Also make sure your route placeholder: /{user} matches the variable name in
the controller: public function show(User $user).
Hope this helps.
I too came across with the same problem.
If your model having two or more words, you have to use only small letters like $modeModel as $somemodel.
public function show(SomeModel $somemodel)
{
...
}

How to use wild card in admin routing in laravel 5

I am using code that is below for admin routing in laravel.
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'Admin\AdminController#home');
Route::get('/users/userList', 'Admin\UserController#userList');
Route::get('/users/detail', 'Admin\UserController#detail');
Route::get('/posts/view', 'Admin\PostController#view');
Route::get('/posts/edit', 'Admin\PostController#edit');
Route::get('/posts/add', 'Admin\PostController#add');
});
This is working fine for me. But when I add new functions in code for that I have to write routing in routes file. For example: If I want to add edit functionality in users controller, for that I have to add new route like .
Route::get('/users/edit', 'Admin\UserController#edit');
So I have to add routing for each function.
I want to know How to use wild card for admin routing so that I have to write routing only for controller not for each function for example.
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'Admin\AdminController#home');
Route::get('/users/:any', 'Admin\UserController#:any');
Route::get('/posts/:any', 'Admin\PostsController#:any');
});
wild card replace the function name, and auto ridirect to that function.
You could use implicit controllers that will do what you need.
First declare a route for your implicit controller
Route::controller('users', 'UserController');
Then, on your controller, you have to follow a convention for naming your routes with HTTP verbs used to access them (get for GET, post for POST, any for both)
class UserController extends Controller {
public function getIndex()
{
//
}
public function postProfile()
{
//
}
public function anyLogin()
{
//
}
}
A note about composed method name from documentation
If your controller action contains multiple words, you may access the action using "dash" syntax in the URI. For example, the following controller action on our UserController would respond to the users/admin-profile URI:
public function getAdminProfile() {}

how is a before filter called from a route on the router instance in laravel

from the documentation
I can call it like this
Route::get('profile', array('before' => 'auth',
'uses' => 'UserController#showProfile'));
The router will create the Route with the property before
In Route->run the following is called
list($name, $params) = $this->parseFilter($name, $params);
if ( ! is_null($callable = $this->router->getFilter($name)))
{
return call_user_func_array($callable, $params);
}
My question is: How does the Router know the name of the filter. I follow the createRoute method?
thanks
I'm not entiely sure on what you are asking - but Laravel has two filters - before and after.
Before filters are run before your route is called. You can attach as many filters to your before as you desire - like this: 'before' => 'auth|other|example'. In this case it will run auth, other and example (in that order).
You can do exactly the same thing with your after filters.
This filters can be applied in the routes file like in your example
Route::get('profile', array('before' => 'auth',
'uses' => 'UserController#showProfile'));
Or they can be applied inside a controller if that is where you perfer
class UserController extends BaseController {
public function __construct()
{
$this->beforeFilter('auth');
}
}
You can also specifically in the controller filters that the filters only apply to specific routes
public function __construct()
{
$this->beforeFilter('csrf', array('on' => 'post'));
}
Or you can say it applies on all routes except specific routes
public function __construct()
{
$this->beforeFilter('auth', array('except' => 'post'));
}
I believe you are asking about Laravel 4.0, just by guessing from the code snippet you have provided. The code in the Routing module is changed quite a bit in Laravel 4.1.
The Router knows the name of the filter because router keep that data!.
Let's start with the creating before filter. When creating before filter before() method in route class called.
public function before()
{
$this->setBeforeFilters(func_get_args());
return $this;
}
In this function, parameters are read by func_get_args function.
Then filter name and filter parameters is set to the internal data store using setBeforeFilters method.
I hope this answers your question. (If I understood it correctly)

Laravel 4 authentication. Restrict access to some functions of a resource but not all

I have this blog resource which has the usual CRUD methods.(index, create, store, show, edit, update, destroy).
I have the following route in my routes.php:
Route::resource('blog', 'PostsController');
but I want to restrict all but index and show.
so I have
Route::get('blog', 'PostsController#index');
Route::group(array('before' => 'auth'), function()
{
Route::resource('blog', 'PostsController');
});
which is fine for index but I don't know how to route the show method ? Or is there another way? Instead of routing the resource should I route every URI individually and put the ones I want restricted in my restricted access route?
Cheers
Laravel has a feature that lets you specify filters in the controllers' __construct method using $this->beforeFilter. This function takes a second argument that lets your provide exceptions (or enable the filter only for certain methods). Try using your original routes file and set up your controller like this:
class PostsController extends BaseController {
function __construct() {
// ...
$this->beforeFilter('auth', array('except' => array('index', 'show')));
// ...
}
// ...
See Controller Filters in the Laravel documentation. It's not entirely well-documented, but you can also start a deeper journey into the guts of Laravel from here.
In Laravel 5 you use middleware function instead like this:
$this->middleware('auth', array('except' => array('index', 'show')));

Categories