our users access our site with a unique parameter on the url. ie http://example.com/hire-agreement?u=unique_param
I've set up a route to a view -
Route::get('/hire-agreement', function () {
return view('hire-agreement');
});
I have 2 questions.
Do I need to add anything else to the Route to allow the parameter to be read in the view?
How do I read this parameter value in the View? Can I use $_GET["name"]) ?
thanks
Craig.
you don't need anything more in the url section. and to use or get url parameter use laravel request() helper.
$value = request('key');
in view you can print a key like
{{ request('name') }}
complete example for you using request helper
Route::get('/hire-agreement', function () {
$name = request('name'); //put the key in a variable
return view('hire-agreement', compact('name')); //send the variable to the view
});
and then in view you can use the variable as
{{ $name }}
if you don't want to use a variable you can use directly request helper in view
{{ request('name') }}
you can use Request class too.
Route::get('/hire-agreement', function (Request $request) {
$name = $request->name;
return view('hire-agreement', compact('name'));
});
however i would suggest you to use a controller. don't use closure in route file. u can't cache them when needed.
http://example.com/hire-agreement?u=unique_param
in laravel you can access both post and get can be access by Request class instance or request() helper so you can do
with helper request()
Route::get('/hire-agreement', function () {
dd(request('u')) // this getting from url ?u=unique_param this u param
return view('hire-agreement');
});
with Class Request
or
Route::get('/hire-agreement', function (Request $request) {
dd($request->u)) // this getting from url ?u=unique_param this u param
return view('hire-agreement');
});
here you can
You better pass the request to a controller and handle it there, it's easier and cleaner that way.however if you want to got straight from route to view, you better use the below method.
put this in your route file
Route::get('/hire-agreement/{param}', function ($param) {
return view('hire-agreement')->with($param);
});
in the view you can access the param like this
<p>{{$param}}</p>
now if user request "/hire-agreement/1234" your $param in the view will contain 1234, Also if you would like to access get parameters in the url you can do it like this
{{Request::input('q')}}
Related
I am new to laravel and actually never used it before
I have a showForm.blade.php where the form is present whose post request is transferred to route web.php
get route
Route::get('/', function () {
return view('showForm',['na'=>'']);
})->name("start");
post route
Route::post('/upload', function (Request $request) {
if($request->file("thing")=="")
{
// return back()->withInput();
return redirect()->route('start');
}
else
{
$name=$request->file("thing")->getClientOriginalName();
Storage::disk("google")->putFileAs("",$request->file("thing"),$name);
$url=Storage::disk('google')->url($name);
$details=Storage::disk("google")->getMetadata($name);
$path=$details['path'];
DB::insert('insert into books (Title, Url, FileId) values (?,?,?)', [$name,$url,$path]);
return redirect()->route('start');
}
})->name("upload");
I want that in post route
when if condition becomes true then along with redirecting to get route, it sends a variable $msg="insert data"; and when else condition becomes true, it sends a variable $msg="success upload"; which the get route will receive and use it in
return view('showForm',['na'=>$msg]);
to return the msg to the showForm.blade.php
how can I do this?
I have read that we can send the parameters by
return redirect()->route('start',['varaiable'=>'value'])
but where to specify that variable in get route so that can use it inside for the message so that the user will receive a different message according to the situation.
I see. It seems like you want to flash the message insert data after redirecting to another route. In that case, you can use the with method.
return redirect()->route('start')->with('message', 'Insert Data!');
Now this message will be kept in the session. On the start route blade template, you can do something as follows -
#if (session('message'))
<h1>{{ session('message') }}</h1>
#endif
The message should show up. Once you refresh the page, it'll go away. Read the official docs for HTTP Session if you want to learn more.
try this:
<?php
namespace Emedico\Http\Controllers;
use Session;
use Redirect;
public function upload(Request $request)
{
return Redirect::route('start', [variable => val];
}
For now in Laravel i am testing url and in route i have
Route::group(['prefix' => 'game'], function (){
Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController#index')->name('game.index');
});
In controller when i wanna pass params i have to type
example.com/game/location/1/action/update/screen/main
if i wanna pass only location and screen i have an error cause in url second param should be action.
I can create url like
example.com/game/?location=1&screen=main
and controller $request->screen and location works fine. But is any way to not using & ? and do this like:
example.com/game/location/1/screen/main
For this route
Route::get('/locations/{location?}/actions/{action?}/screens/{screen?}','GameController#index')->name('locations.actions.screens.show');
In GameController index method, you can get these parameter as
public function index(Location $location, Action $action, Screen $screen) {
// here you can use those models
}
if you are using route-model binding,
if not using
public function index($location, $action, $screen) {
// here you can use these variables
}
If the route name is locations.actions.screens.show then in view, it will be
Test
Now, if you have some query parameter
then it will be like $http://example.com/?test="some test data"&another_test="another test"
you can access these parameter like
public function myfunction(Request $request) {
dd($request->all());
}
Let's consider you want to retrieve all games, that belongs to a particular screen which belongs to a particular action and that belongs to a particular location, what your urls seems to be in your question, in that case, the url will be
Route::group(['prefix' => 'game'], function (){
Route::get('locations/{location?}/actions/{action?}/screens/{screen?}','GameController#index')->name('game.index');
});
url seems to be game/locations/1/actions/1/screens/1 where action and screen parameter can be optional
now in your controller index() method
public function index(Location $location, Action $action=null, Screen $screen=null) {
//using the model instance you received, you can retrieve your games here
}
Your error makes sense
URL second param should be action
because your route is with wild card location, action, screen
Route::group(['prefix' => 'game'], function (){
Route::get('/location/{location?}/action/{action?}/screen/{screen?}','GameController#index')->name('game.index');
});
To access this route you have to generate a URL with a wild card like
example.com/game/location/1/screen/main
and example.com/game/?location=1&screen=main not going to work because of your route URL and you can't access like $request->screen.
so your controller must be like
public function index($reuest, $location, $action, $screen){
}
You can directly access $location, $action, $screen and if you request something like
example.com/game/location/1/screen/main?param1=1¶m2=2
Those are accessible through request like
$request->param1 and $request->param2
Occasionally you may need to specify a route parameter, but make the presence of that route parameter optional. You may do so by placing a ? mark after the parameter name. Make sure to give the route's corresponding variable a default value:
Route::get('user/{name?}', function ($name = null) {
return $name;
});
you can use Pattern Based Filters
You may also specify that a filter applies to an entire set of routes based on their URI.
Route::filter('admin', function()
{
//
});
Route::when('admin/*', 'admin');
I am trying to pass on the value $id through my href to the next page
Route:
Route::get('/offer-me/{id}', 'OffersController#create')->name('project.detailed');
From View:
<button class="btn btn-success">View</button></p>
When I go to localhost/projectp0/public/offer-me/2 .... There is no $id value?
{"_token":"dSxgM8wTlpNxhDKsqj713KMy656bg8XAU5Q2sqe4","_previous":{"url":"http:\/\/localhost\/projectp0\/public\/auction"},"_flash":{"old":[],"new":[]},"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d":1}
To get that view i Run:
$data = session()->all();
return($data)
The {id} parameter lives in the request, and not in the session.
It will be available as request()->id.
The Returned Data set is your session data. Session data is not storing the data you send from a Get Method. Get method send Data with the URL,
So create method in Offer Controller should be like this.....
public function create($id){
dd($id);
}
Then You Can see What is the value passed for the $id,
You could use
public function create($id)
{
}
It is better to have a create route use a post route
in which case you should be able to access it through the request
public function create(Request $request)
{
$id = $request->id
}
as #drew010 suggested you in comments, try this way :
<button class="btn btn-success">View</button></p>
I use laravel 5.6
I have GET parameter which I want to pass to redirect function.
Route::get('/about', function () {
//I want to add param to this redirect function
return redirect('/en/about');
});
if the route looks like /about?param=123 after redirect the param will be lost. is there way to add parameter to redirect method? as I see this function doesn't include input parameters. the parameter is optional, so it may not be provided. maybe there's way to override this function? or some other solution? all suggestions will be appreciated
UPDATE
is it possible to override the redirect() method ? I think in my case it will be the best solution
You have to get the parameter in the URL and pass it to redirect method in an array
Route::get('/about/{param}', function () {
return \Redirect::route('/en/about', ['param'=>$param])
});
without having to use named route
Route::get('/about/{param}', function () {
return redirect('/en/about', ['param'=>$param])
});
For optional parameter
Route::get('/about/{param?}', function ($param = 'my param') {
return redirect('/en/about', ['param'=>$param])
});
Yeah, you can redirect to named routes and pass parameters, like this:
return redirect()->route('en.about', ['param' => 123]);
just do something like this:
return redirect('/en/about?param='.$param);
If you don't want to add route name then you can do the same with controller function
Route::get('/about/{param}', function () {
return \Redirect::action('CONTROLLER#FUNCTION',['param'=>$param])
});
OR with the helper function
return redirect()->action('CONTROLLER#FUNCTION');
Its best to do it this way in your case:
return redirect(route("en.about")."?param=123");
Route::get('/about', function () {
//I want to add param to this redirect function
return redirect()->to(url('/en/about',['param' => 'Pram vakue', 'param2' => $param]));
});
If you use a route() then you have to create a named route.
Hope this helps
My current url I'm on is localhost/test/1
When I click on save button, it's going to call a rest service test/{id}/createNewTest. How do I get the {id} in the controller? Right now it's getting nothing.
Routes
Route::post('test/{id}/createNewTest', 'TestController#createNewTest');
Controller
public function createNewTest() {
$id = Request::input('id');
}
the $id is suppose to be 1 here, but it's getting nothing
Route parameters are passed into the controller via method parameters, not as request input:
public function createNewTest($id) {
var_dump($id); // from method parameter, not Request::input()
}