Laravel 4 magic method __call replacement in controllers - laravel

I am building a permission system and I need to have granularity over every method of every controller, so i was thinking to implment this with the __call magic method on my base controller like so:
public function __call($name, $args)
{
if ( $this->checkPermission() )
{
call_user_func_array(array($this, $name), $args);
}
else
{
// handle error
}
}
But apparantly this does not work in Laravel 4. How would be right approach to emulate that __call magic method ? I thought of before filters but they are not handed the called method name and arguments

__call is a magic method that is called when the method does not exist on the class. So I don't see how that would help you.
My suggestion would be to use a before filter, as you do have access to the current route and request.
Route::filter('permissions', function($route, $request)
{
});
You could then use methods like $route->getAction() to extract the controller and method that will be called and $route->getParameters() or $request->segment() to get the arguments.
Just register all routes inside a group that has this filter applied.
Route::group(array('before' => 'permissions'), function()
{
Route::get('/', function() { });
});

Related

Laravel: What are functions in routes doing?

Can anyone tell why the documentation of Laravel, and others, show functions in routes that return / do something? In what context can you use this?
For example, I try to figure out Molly Connect.
Here is the corresponding code from https://github.com/mollie/laravel-mollie/blob/master/docs/mollie_connect.md
Route::get('login', function () {
return Socialite::with('mollie')
->scopes(['profiles.read']) // Additional permission: profiles.read
->redirect();
});
Route::get('login_callback', function () {
$user = Socialite::with('mollie')->user();
Mollie::api()->setAccessToken($user->token);
return Mollie::api()->profiles()->page(); // Retrieve payment profiles available on the obtained Mollie account
});
Its just a shortcut, to avoid having to create separate controller files and indirectly referencing those functions. Functionally, your example is no different from doing this:
Route::get('login_callback', 'LoginController#callback')
And then, LoginController.php
class LoginController
{
public function callback()
{
$user = Socialite::with('mollie')->user();
Mollie::api()->setAccessToken($user->token);
return Mollie::api()->profiles()->page();
}
}
See here

How can I implement Resource Controllers if I use many "get" on the laravel?

I have routes laravel like this :
Route::prefix('member')->middleware('auth')->group(function(){
Route::prefix('purchase')->group(function(){
Route::get('/', 'Member\PurchaseController#index')->name('member.purchase.index');
Route::get('order', 'Member\PurchaseController#order')->name('member.purchase.order');
Route::get('transaction', 'Member\PurchaseController#transaction')->name('member.purchase.transaction');
});
});
My controller like this :
<?php
...
class PurchaseController extends Controller
{
...
public function index()
{
...
}
public function order()
{
...
}
public function transaction()
{
...
}
}
I want to change it to Resource Controllers(https://laravel.com/docs/5.6/controllers#resource-controllers)
So I only use 1 routes
From my case, my routes to be like this :
Route::prefix('member')->middleware('auth')->group(function(){
Route::resource('purchase', 'Member\PurchaseController');
});
If I using resouce controller, I only can data in the index method or show method
How can I get data in order method and transaction method?
You could try like this, Just put your resource controller custom method up resource route.
Route::prefix('member')->middleware('auth')->group(function(){
Route::get('order', 'Member\PurchaseController#order')->name('member.purchase.order');
Route::get('transaction', 'Member\PurchaseController#transaction')->name('member.purchase.transaction')
Route::resource('purchase', 'Member\PurchaseController');
});
For the resource controller, it is pre-defined by the Laravel, which contain only the 7 method.
Shown at below table.
So, if you want any other method, you have to definde by youself.
php artisan route:list
You can use this to check all the route you defined.
The other answers on here are pretty much correct.
From my other answer you linked this question in from, here that way based on what MD Iyasin Arafat has suggested, if you are using laravel 5.5+:
# Group all routes requiring middleware auth, thus declared only once
Route::middleware('auth')->group(function(){
# Suffix rules in group for prefix,namespace & name with "member"
Route::namespace('Member')->prefix('member')->name('member.')->group(function () {
Route::get('purchase/order', 'PurchaseController#order')->name('purchase.order');
Route::get('purchase/transaction', 'PurchaseController#transaction')->name('purchase.transaction');
Route::resource('purchase', 'PurchaseController');
});
});
Grouping Methods ( ->group() ) :
Controller Namespace ( ->namespace('Member') )
Prepends to 'PurchaseController' to give
'Member\PurchaseController'
Route Name (->name('member.'))
Prepends to name('purchase.order') to give
route('member.purchase.order')
URI Request (->prefix('member'))
Prepends to /purchase to give example.com/member/purchase
As you can see, using the methods above with group() reduces repetition of prefix declarations.
Hint
Custom routes must always be declared before a resource never after!
Example to use if you have a lot of custom routes for Purchase Controller and how a second controller looks for member group:
# Group all routes requiring middleware auth, thus declared only once
Route::middleware('auth')->group(function(){
# Suffix rules in group for prefix,namespace & name with "member"
Route::namespace('Member')->prefix('member')->name('member.')->group(function () {
Route::prefix('purchase')->name('purchase.')->group(function() {
Route::get('order', 'PurchaseController#order')->name('order');
Route::get('transaction', 'PurchaseController#transaction')->name('transaction');
Route::get('history', 'PurchaseController#history')->name('history');
Route::get('returns', 'PurchaseController#returns')->name('returns');
Route::get('status', 'PurchaseController#status')->name('status');
Route::resource('/', 'PurchaseController');
});
Route::prefix('account')->name('account.')->group(function() {
Route::get('notifications', 'AccountController#notifications')->name('notifications');
Route::resource('/', 'AccountController');
});
});
});

Implicit route, with firstOrCreate instead of findOrFail

Is it possible to create an implicit route where if it is not found the thing is created? I am using Laravel 5.5.13.
For instance this is my implicit route:
Route::post('thumbs/{player}', 'ThumbController#store');
And in my controller it is this:
public function store(Request $request, Player $player)
{
$thumb = new Thumb($request->all());
$player->thumbs()->save($thumb);
return response()->json($thumb, 201);
}
So now if I go to the endpoint of ..../api/thumb/1 it will create a thumb related with Player of id 1. However instead of a id number I wanted to provide it a string like this:
..../api/thumb/PLAYER_NAME
So example of ..../api/thumb/Blagoh, then my endpoint should first find if a player exists by name "Blagoh", and if it doesn't then it should create it. I couldn't figure this one out.
What you should do is adding into boot method of RouteServiceProvider something like this:
Route::bind('player', function ($value) {
if ($player = \App\Player::find($value)) {
return $player;
}
return Player::create(['name' => $value]);
});
It's called explicit binding and you can update logic as showed above. Reference - Route model binding

My Own Controller Method in Auth Facade in Laravel 5.1

I want to add my own controller method to Auth Facade in Laravel
auth()->user()->ownControllerMethod()
How can we do that ?
In your example, you'd actually be adding a method to the User model. (User.php)
public function my_method() {
// code here
}
EDIT:
Per your comment, lets say your route is mapped to the index() method in your UsersController, you could do this:
public function index()
{
// some code
$this->doSomething();
}
protected function doSomething()
{
// some code
// return data
}
But it really depends what this method is meant to do. There's lots of different ways to break up your code. And depending on the size of your project, your User model maybe a fine place.

Passing Route Parameters to Filters when Using RESTful Controllers

I have searched around for long but nothing can quite fit my problem.
I am using RESTful controllers on my site. For some controller actions, some filters are needed and with this i do something like (i use the beforeFilter() function in the constructor):
<?php
class PostController extends BaseController {
public function __construct()
{
$this->beforeFilter('auth',array('only'=>array('getCreate','postCreate','getEdit','postEdit','postAddComment')));
$this->beforeFilter('csrf',array('only'=>array('postCreate','postEdit')));
// $this->beforeFilter('auth_post_edit', array('only'=>array('postEdit','getEdit')));
}
public function getIndex
{
$posts = Post::all();
return View::make('home')->with('posts',$posts);
}
public function getCreate()
{
return View::make('posts.create');
}
...
For the commented filter, however, it is meant to ensure that only the author of a post can edit the post, so i need to pass the post_id which is passed as a URI parameter, to the filter(or access it from the filter).
Some link showed how i can access parameters from the filter using the $route->getParameter('param') in the filter, but the problem is that because i have not even named my parameters(they are named in the controller actions), i am not able to access them from the filter using the above method.
So, how can i access route parameters from within the filter, or/and how do i name route parameters in RESTful controllers(not in their actions)?
You could use in your filter the Request::segment()
Route::filter('foo', function()
{
$param=Request::segment(2); //if the parameter is on the 2nd uri fregment.
$post=Post::find($param);
if ($post->author_id != Auth::user()->id)
{
return App::abort('404'); //or whatever
}
});

Resources