How to get controller action by passing URL in laravel - laravel

I searched more time to find how to get the controller method name by passing the URL but not found my expected answer. I want to make a method where I will pass a URL and it will give the corresponding controller action like as below but I can't figure out.
I found a helper which just return the current URL's action which is Route::currentRouteAction()
If a route in my application like as Route::get('/abc', 'YourController#method') which will generate the url http://example.com/abc
then how can I get the YourController#method by passing http://example.com/abc
function getAction($url){
//what will be logic?
// return like App\Controllers\MyController#method
}
I have to make a custom permission system where I need it for show and hide the menu by checking the URL of each menu.

Within your controller you can do the following:
<?php
use Illuminate\Routing\Router;
use Illuminate\Http\Request;
public function index(Request $request, Router $route)
{
$action = $router->getRoutes()->match($request)->getActionName();
// action should be what you're looking for.
}

You can try this if you want to:
Route::get('/the/url', 'YourController#method');
Every time anything calls the URL in the route, your method will be called.
You don't need to navigate to that url to call your method, it could be called by a form action, or a buttons action and just execute your method.
Edit:
url is your url as parameter (plain route)
import this:
use Illuminate\Routing\Route;
this is your function:
public function method(Route $route, $url)
{
$routes = \Route::getRoutes()->getRoutes();
foreach($routes as $r){
if($r->getUri() == $url){
$youraction= $r->getActionName();
dd($youraction);
}
else{
dd('does not exist');
}
}
}
Tested.

Related

Laravel: Multiple Route to Same Controller

May I know how can I make just a single route so I don't have to repeat it? Thanks in advance.
Route::get('/url', 'CtcFormController#index' )->name('CtcForm.ch');
Route::post('/url/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/url/submitted', 'form.submit')->name('CtcForm.submit');
Route::get('/url2','CtcFormController#store')->name('CtcForm.eng');
Route::post('/url2/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/url2/submitted', 'form.submit')->name('CtcForm.submit');
As per your given example, you want to handle the variable part of the route which is /url/ and /url12/. Yes! you can handle there both different route using a single route in ways:
Use route variable to handle dynamic url values i.e. url, url2,url3...url12 and so on.
Route::get('/{url}', 'CtcFormController#index' )->name('CtcForm.ch');
Route::post('/{url}/submit', 'CtcFormController#submit')->name('CtcForm.submit');
Route::view('/{url}/submitted', 'form.submit')->name('CtcForm.submit');
Now in your controller methods handing above routes receive extra parameter $url like:
In controller CtcFormController.php class:
public function index(Request $request, string $url) {
//$url will gives you value from which url request is submitted i.e. url or url12
//method logic goes here ...
}
Similarly, method handling /{url}/submit route will be like:
public function submit(Request $request, string $url) {
//method logic goes here ...
}
Let me know if you have any further query regarding this or you face any issue while implementing it.

Laravel new controller method doesn't work

When I create a new function in the controller for some reason it does not work. When I set the code from this function(getUnitsNotIn) to another function(index), that code works.
Does anyone know why this is happening?
My UnitsController.php action
public function index(){
$items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
return Units::whereNotIn('id',$items)->get();
}
public function getUnitsNotIn(){
$items = Unit::select('parent_id')->where('parent_id','!=',NULL)->get()->toArray();
return Units::whereNotIn('id',$items)->get();
}
My api.php
Route::apiResource('/units', 'UnitController');
Route::get('/units/notIn', 'UnitController#getUnitsNotIn');
In short, any new controller function that I make will not work. I tried to make a new controller and the same thing happens.
How to fix this problem?
It is not working because of apiResource(). Resource route assumes /units/{id}. so when you call /units/notIn route assume notIn as id And Action call show()
You need to use a different name.
Route::get('/un/notIn', 'UnitController#getUnitsNotIn');
Verb, path , action , route name
GET /units/{id} show units.show
Change this to
Route::get('/units-notIn', 'UnitController#getUnitsNotIn');
You define a resource controller so here in units/notin,notin define a id so
it call your show function default.

Trying to get intended url in LoginController in Laravel 5.6

I'm trying to capture the intended url in my LoginController so I can execute some logic in a showLoginForm() method I added to the controller so I can send the user to a specific view based on the intended URL.
I've tried the following and I cannot get it to work:
public function showLoginForm()
{
$intededUrl Session::put('url.intended', URL::full());
// my base application url is http://www.websites.com:8080
if (starts_with($intededUrl, url('/admin'))) // i want all routes that begin with http://www.websites.com:8080/admin to go here
return view('auth.login');
return view('themes.'.env('APP_THEME', 'mango').'.auth.login'); // but it keeps taking me here
}
I'm using Laravels starts_with() method to try and match the start of the url string.
I just figured it out. I needed Session::get('url.intended');

How Redirect to Other Method in one Controller with Request in laravel?

in my case i have a route for get and set API.
if user want to get something i don't want to check Validation. but if his wants to set, i Want to Check Request input validation with Request file.
look:
class EventsController extends Controller
{
public function get(Request $request)
{
if( empty($request['data']) )
{
// Return Request.. is ok
}elseif( !empty($request['data']) && $request->has('data.id') )
{
// so User want to insert in database and I want to check
// Validation with Request file in the method
// How can i Do this?
call $this->store( // send Request to that for Validation )
}
}
public function store(ValidateInput $request)
{
// Insert into Database
}
}
Note: in the getMethod i don't want check validation but in store method i want!
1- i don't want to use other Route and i want do Both in one Request and Route
2- my main Question: who can i change Method in Controller and pass Request to that!
you can try something like :
//Calling a method that is from the EventsController
$result = (new EventsController)->store();
but the best approche is to split them into two methods as #Sandeesh said

Dynamic router name for magento controller

How would I go about creating a custom module that has a controller with an action name that is dynamic, in the sense that it can be configured by the user in the admin area at will and be automatically updated in the custom module?
You can override this method in your controller:
public function getActionMethodName($action)
{
return 'indexAction';
}
public function indexAction()
{
//action name
var_dump($this->getRequest()->getActionName());
}
Then always will go to the index action, where you can use the original action name as a parameter.
then:
http://mysite/mymodule/mycontroller/im-dracula-blablabla
Will work!
I think you can approach this by using magic php method __call on your controller.
I assumed that you store your action name in a Magento config named 'mymodule/controller/action', so you can get the value using :
Mage::getStoreConfig('mymodule/controller/action');
Then you have the controller for example Mymodule/controllers/TestController.php
And you add the method in that controller like this :
public function __call($method, $arg) {
if ($method == Mage::getStoreConfig('mymodule/controller/action')) {
//Do whatever you want
}
}
This will make your controller //Do whatever you want when you accessing it using the action you specified in the config. The basic idea is like that. Hope this helps.

Resources