Get the action name in Laravel - laravel

I need to get the action name in laravel:
mysite.com/manager/user-list
So in the above example I want 'user-list'
I know you can get the current route name by:
Route::currentRouteName();
But this is no good to me, I need this to stay as 'manager' for other reasons.

If you want to get URL segment (not controller action), you can call segment method on Request Facade.
Request::segment(2);

i don't know what you are trying to do but this is one of the methods to get the action.
print_r(explode('#',Route::currentRouteAction()));
Output
Array ( [0] => HomeController [1] => index )
so 1st element will hold the controller (provided not namespaced, otherwise you will have to remove the namespace part manuelly) and 2nd element will hold the action.
your question and the explanation doesn't match.
if you want the action, this is one of the process.
but i think, Andreyco's answer gives you what you need. i just put it for future reference if someone finds it helpful.

https://laravel.com/docs/5.3/routing#accessing-the-current-route
The link above shows you how to solve your problem:
$action = Route::currentRouteAction();

Related

How to return to another page after finishing process in Laravel?

This is a little bit hard to understand even the title I put. Sorry about that I just do not know how to clearly explain this, but I will try...
So for example I have a controller and inside that controller I have a function which return the data in the table of my database. then in the last column of every row, I make view,add,edit,delete options as links. When a user clicks on the add for example, they will redirect to an add page. After they submit the form of the add page. they should be redirected to the first page that return the data from the table. but the problem is, the variables of foreach loop in the first page do not got recognized by laravel anymore. because they do not got processed since the route does not goes to the controller and to the function that return the data instead it goes to add function.
So I want to know is there anyway to solve this? If you could provide sample code, I would appreciate a lot thanks
From your explanation, I believe the code to go back to the original page after adding, editing etc is simply return redirect()->back(). This will take the user back to the previous page. As for data continuity, one approach would be considering using sessions. They save data globally and can be accessed from any controller once saved. To do this, simply save the data with session(['session_name' => $data]) and to retrieve the data use session('session_name'). Let me know if this helps!
If you want ti redirect after something like a login or an activation process you can do it with something like this:
return redirect()->to('login')
You can specify the path from your web.php file as you can see in my example in 'myPath'
As #DerickMasai correctly pointed out it is better to use named routes instead of hard coding the path itself.
Naming a route can work like so:
Route::get('/login', [LoginController::class, 'index'])->name('login');

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

parse variable from view to controller laravel-backpack

im using Laravel-Backpack. could we parse some param from view to backpack controller? imagine i have some list of a package. each package has 5 round so every package has 5 button in its row. when i click first button(round 1) it will open the list of item that has round_id = 1.
what can i think is each round button in package list will parse id to route that will call backpackCrud Controller. and that id will used for advance queries for the item list
my button
<i>2</i>
my route
CRUD::resource('package/round/{RoundId}', 'Admin\CrudController');
my crud controller
$round= \Route::current()->parameter('RoundId');
$this->crud->addClause('where', 'round', '=', $round);
but it return an error
Route pattern "/admin/package/round/{RoundId}/{{RoundId}}" cannot reference variable name "RoundId" more than once.
what i know is we cannot parse param to restfull controller cause restfull is for quick crud. so what should i do for parse id from view to controller. or maybe there is beautifull way to make the queries? i trully appreciate that
Many thanks!
i found the way to avoid that.
it CANNOT be like:
CRUD::resource('package/round/{RoundId}', 'Admin\CrudController');
we have to follow the pattern of the default route like
CRUD::resource('someRoute/{someId}/someSecondRoute{someSecondId}', 'Admin\CrudController');
with that way, we can get the first and second paramater in controller with this:
$firstParam= \Route::current()->parameter('someID');
$SecondParam= \Route::current()->parameter('someSecondID');
hope this help someone else ;)

How to pass route values to controllers in Laravel 4?

I am struggling to understand something that I am sure one of you will be able to easily explain. I am somewhat new to MVC so please bear with me.
I have created a controller that handles all of the work involved with connecting to the Twitter API and processing the returned JSON into HTML.
Route::get('/about', 'TwitterController#getTweets');
I then use:
return View::make('templates.about', array('twitter_html' => $twitter_html ))
Within my controller to pass the generated HTML to my view and everything works well.
My issue is that I have multiple pages that I use to display a different Twitter user's tweets on each page. What I would like to do is pass my controller an array of values (twitter handles) which it would then use in the API call. What I do not want to have to do is have a different Controller for each user group. If I set $twitter_user_ids within my Controller I can use that array to pull the tweets, but I want to set the array and pass it into the Controller somehow. I would think there would be something like
Route::get('/about', 'TwitterController#getTweets('twitter_id')');
But that last doesn't work.
I believe that my issue is related to variable scope somehow, but I could be way off.
Am I going down the wrong track here? How do I pass my Controllers different sets of data to produce different results?
EDIT - More Info
Markus suggested using Route Parameters, but I'm not sure that will work with what I am going for. Here is my specific use case.
I have an about page that will pull my tweets from Twitters API and display them on the page.
I also have a "Tweets" page that will pull the most recent tweets from several developers accounts and display them.
In both cases I have $twitter_user_ids = array() with different values in the array.
The controller that I have built takes that array of usernames and accesses the API and generates HTML which is passed to my view.
Because I am working with an array (the second of which is a large array), I don't think that Route Parameters will work.
Thanks again for the help. I couldn't do it without you all!
First of all, here's a quick tip:
Instead of
return View::make('templates.about', array('twitter_html' => $twitter_html ))
...use
return View::make('templates.about', compact('twitter_html'))
This creates the $twitter_html automatically for you. Check it out in the PHP Manual.
 
Now to your problem:
You did the route part wrong. Try:
Route::get('/about/{twitter_id}', 'TwitterController#getTweets');
This passes the twitter_id param to your getTweets function.
Check out the Laravel Docs: http://laravel.com/docs/routing#route-parameters

passing data to another controller

I need to pass product Id from cart to custom controller, for example
http://**/catalogsearch/filter/results?product={ID}
I dont have a clue how to pass data like that using magento helpers etc
Thanks
I am assuming from your question that you are redirecting from one controller to another and would like to pass query parameters along?
The syntax would be:
$params = array('key' => 'value');
$this->_redirect('frontname/controlller/action', array('_query' => $params));
EDIT
To answer the question in the comment on how to get these parameters from a template:
First off, I would advise not to do this, you should be recieving parameters in the controller. So, your custom controller should be responsible for receiving any parameters.
Regardless, the code in either situation is the same:
All parameters…
$this->getRequest()->getParams()
Single parameter…
$this->getRequest()->getParam('parameter_name')

Resources