How create one route with multi method in there? - php

I would like create one route with index method and all method name if exist can be call. Is any way to do this ?
Route::get('/{params}/table', TableController#index)->name(table.index)
Now in controller i would like create index method and all request should go there. I want get params as method and try call if exist.
public function index(Request $request) {
dd($request);
}
How can i do this ?
UPDATE
I think i need smth like:
Route::get('/{params}/abc/{any?}, function(any) {
get params $any
call function from TableController#any
});
and if method exist in TableController

You can simple receive $params or $any variable in the controller
public function index($params, $any) {
dd($any);
}
and than check if needed method exists.

Related

How To Get Laravel Controller Method Name & Method Type Automatically

Basically I'm calling an event every time a Controller method runs:
public function destroy(User $user)
{
event(new AdminActivity('admin.users.destroy',class_basename(Route::current()->controller),'destroy','DELETE'));
...
}
In fact it is saving this information:
event(new AdminActivity(ROUTE_NAME,CONTROLLER_NAME,CONTROLLER_METHOD_NAME,CONTROLLER_METHOD_TYPE));
Now instead of passing parameters manually, I want to pass the required parameters automatically.
So I need to get route name, controller method name and controller method type auto (just like the class_basename(Route::current()->controller) which returns the controller name).
So how can I do that ?
You can pass Route::current() to the event and then get your required information from the object of \Illuminate\Routing\Route
public function destroy(User $user)
{
event(new AdminActivity(\Illuminate\Support\Facades\Route::current()));
...
}
Then, in your AdminActivity event class
class AdminActivity
{
public function __construct(\Illuminate\Routing\Route $route)
{
$controllerClass = class_basename($route->getController());
$controllerMethod = $route->getActionMethod();
$routeName = $route->getAction('as');
$methods = $route->methods();
}
}
Notice: return type of $route->methods() is an array that contains all valid request methods (GET, HEAD, POST, ...)

Get request from controller in model - Laravel

In my API I used "with" method to get parent's model relation and everything works fine.
I want to add an attribute in my relation and return it in my API but I should use request in my model.
Something like this :
Book.php
protected $appends = ['userState'];
public function getUserStateAttribute () {
return User::find($request->id); //request not exists here currently
}
I have $request in my controller (api controller)
Controller.php
public function get(Request $request) {
Post::with('books')->all();
}
I believe using static content to append in array of model is so easy but how about using request's based content ?
I guess you can use request() helper :
public function getUserStateAttribute () {
return User::find(request()->get('id'));
}
Sure this is not really MVC pattern, but it can work
You want to take request as a parameter here:
public function getUserStateAttribute (Request $request) {
return User::find($request->id);
}
That way you can access it in the function. You will just need to always pass the Request object whenever you call that function.
e.g. $book->getUserStateAttribute($request);
Alternatively, you could just pass the ID, that way you need not always pass a request, like so:
public function getUserStateAttribute ($id) {
return User::find($id);
}
Which you would call like:
e.g. $book->getUserStateAttribute($request->id);

laravel legacy route url

i have 2 routes with POST methods
Route::post('/payment/checkOrder','Finance\PaymentCallbackController#checkOrder');
Route::post('/payment/paymentAviso', 'Finance\PaymentCallbackController#paymentAviso');
how can i create legacy links for these routes?
/plat.php?paysystem=5&method=checkOrder
/plat.php?paysystem=5&method=paymentAviso
You can have a single route that recieves a method string, and then call the desired functions according to it.
Route::post('/payment/{method}','Finance\PaymentCallbackController#handler');
// PaymentCallbackController.php
public function handler(Request $request){
// make sure to validate what methods get sent here
$this->{$request->method}($request);
// use $this if its in this controller, for otherControllers
// try something with the looks of app('App\Http\Controllers\OtherControllerController')->{$request->method}->($request);
}
Add this route:
Route::post('/plat.php', 'SomeController#action');
In your controller function:
// SomeController.php
public function someAction()
{
$paysystem = $request->query('paysystem');
$method = $request->query('method');
// some logic here
return view('something');
}

Laravel 5: PHP variable shared by all methods of controller

I have a resource controller. The route for that is like this-
Route::resource('branches/{branch_id}/employees', 'EmployeeController');
The problem is in every method I need to pass the branch variable to the view.
public function index($branch_id){
$branch = Branch::find($branch_id);
$employees = Employee::orderBy('created_at', 'desc')->get();
return view('employees.index', compact('branch', 'employees'));
}
Is there any way that I can pass the branch variable to each view returned through this controller?
#Sapnesh Naik Its not a duplicate as I need to manipulate the branch in each function.
In this case, you may try this:
public function __construct(Request $request)
{
view()->share(
'branch', Branch::find($request->route('branch_id'))
);
}
You may also use request()->route('branch_id') if you don't use method injection by type hinting the Request $request in your __construct method.
Add this to your controller constructor:
public function __construct()
{
view()->share('branch', Route::current()->getParameter('brach_id'););
}

How call specific function dynamically that we get in url/path in laravel

My Code in route.php:-
Route::get('/register/{id}',array('uses'=>'UserRegistration#id));
I want to call function id (that can be any function of controller) in UserRegistration controller.
Url is like this:- http://localhost:8000/register/test,
http://localhost:8000/register/login
here test and login are function in controller.
{id} is the parameter you're passing to route. So, for your routes go with something like this:
Route::get('/register/id/{id}',array('uses'=>'UserRegistration#id));
//this route requires an id parameter
Route::get('/register/test',['uses'=>'UserRegistration#test]);
Doing this you can call your functions but it is not the recomended way of doing it. Having separete routes foreach function allows for a more in depth control.
public function id(Request $request)
{
return $this->{$request->id}($request);
}
public function test(Request $request)
{
return $request->all();
}

Categories