Laravel usage of get and view in Route - laravel

When should I use get and when should I use view? How to decide clearly?
Route::view('/contact','contact',['name'=>'Superhuman'])->name('contact');
Route::get('/contact/{name}','ContactController#index');

If your route only needs to return a view, you may use the
Route::view method. Like the redirect method, this method provides
a simple shortcut so that you do not have to define a full route or
controller. The view method accepts a URI as its first argument and
a view name as its second argument. In addition, you may provide an
array of data to pass to the view as an optional third argument:
For more information you can visit the official docs
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Laravel']);

Related

Whats the difference between redirect and this in Codeigniter?

I am new in Codeigniter and it's one of the good frameworks of php. But on some conditions I'm confused. Like this one. If any of you have any clarification about my dough, it's a great help for me.
Offcouse redirects refresh the page and $this not but apart from this I want to know - anyhow both of them used to go to somewhere else on view pages or like in other controller or in same controller to other methods.
But we don't use these side by side because when getting any of them it will go to that page or method without checking the next lines.
In case of a normal difference then have lot's of but I just want to know about the condition of going to next page or method when we use redirect or $this like this -
$this->Function($value); //It's method of same controller.
redirect('Controller/function'); //It's also doing same with page reload.
Thank for looking my problem.
Redirect()
When you will call any function of helper in codeigniter then you can call function directly without using any object. Helper in Codeigniter is collection of functions.
Redirect() method is a part of URL helper in Codeigniter.
For your ref. https://www.codeigniter.com/user_guide/helpers/url_helper.html
So, just load helper using $this->load->helper('url'); or you can also mention in autoload.php file.
$this->Function(); used to call a function from same controller
$this->Function(); used to call a function from same controller
redirect()
While building a web application, we often need to redirect the user from one page to another page. CodeIgniter makes this job easy for us. The redirect() function is used for this purpose.
redirect($uri = '', $method = 'auto', $code = NULL)
The first argument can have two types of URI. We can pass full site URL or URI segments to the controller you want to direct.
The second optional parameter can have any of the three values from auto, location or refresh. The default is auto.
The third optional parameter is only available with location redirects and it allows you to send specific HTTP response code.
Redirect means jumping to another function mentioned in the redirect method.
$this->Function($value); => jumping to another function and you can execute the code of the same function as well as pass the value back by returning value.
When you send request to codeigniter generally CI controller gets called and then function which is mentioned in uri segment. like below... So this will be another request.
redirect('Controller/function'); //It's also doing same with page reload.
But when you have to call another function within the same request then you can use below approach
$this->Function($value); //It's method of same controller.
This will execute the given function and return the value within same request.

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 :)

Getting the controller?

In my view I create links via:
URL::action('NotSureWhatController#getIndex', 'id') }}
My view is a template that is used by a variety of different controllers, what's the best way to change the name of the controller in the action?
The only thing I can think of is setting a var in the controller and passing it through.
Is there a better way? Or a way to get the controller name?
I can't use 'as' in the route to name the controller either (as this is used for something else) so this won't work:
Route::currentRouteName()
Option 1
You should create the URL directly in the controller, then pass it as a variable to the view. The view will just print the url.
Option 2
You pass the name of the controller as a variable to the view (always from the controller), then you use the escape values of the blade templating to print it inside your function to generate URLs.
Option 3
Using the REQUEST class to get information about the page.

Get the action name in 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();

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