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');
Related
I've been using Laravel-5.8.35. I was invoking a GET request through a form. On my route, I redirected the form action to the same controller where the form was submitted but redirected through a different route, as,
$router->get('/merchant/sd-refund', 'Reports\ReportController#refundSecurityDeposit');
And, in my refundSecurityDeposit method, I called my SohojSdRefundService service,
public function refundSecurityDeposit(Request $request)
{
// $userId, $reason, $sdAmount was fetched from the $request instance
$this->execRefundRequest($userId, $reason, $sdAmount);
}
public function execRefundRequest($userId, $reason, $sdAmount)
{
// here the API service request data was handled,
// and stored in $data instance
return SohojSdRefundService::callSdRefundApi($data);
}
While my SohojSdRefundService service was done handling, I wanted to redirect the route to another route, as,
class SohojSdRefundService
{
public function __construct()
{
}
public static function callSdRefundApi($requestData)
{
// call to other methods inside the class to handle the API response,
// and then return to the same route which isn't working
return redirect('/admin/merchant/list');
}
}
Respectively, instead of redirecting to that route, the page happens to be still on the /merchant/sd-refund?... where the form was submitted initially. I redirected another service like this, which is working fine though. Could anyone suggest what I could be implementing wrong here? TIA.
You need to return a result in refundSecurityDeposit fucntion
public function refundSecurityDeposit(Request $request)
{
return $this->execRefundRequest($userId, $reason, $sdAmount);
}
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')}}
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()
}
I have a url www.mywebsite.com/store/123456
where store is my controller class and I have a index function in it where Im getting the value after after store ,ie 123456.But im not able to achieve it.
As found online,I have tried adding this to routes $route['store/(:any)'] = 'store/index'; also tried
$route['store/(:any)'] = 'store/index/$1';
but doesn't seem to work.Please help me out.
In controller index function is
public function index()
{
echo $this->uri->segment(1);
}
But its not working.Pleae help
You are invoking 123456() method instead of index() method therefore you get CI's 404.
The simplest way is to use this kind of route
$route['store/(:any)'] = 'store/index/$1';
AND in top of it add parameter to index function in your case
public function index($parameter)
{
echo $this->uri->segment(2);
}
note that I changed segment parameter please see documentation.
using _remap()
function _remap($parameter){
$this->index($parameter);
}
function index($p)
{
echo $p; //shows 123456
}
If I remember correctly:
segment(n) gives you the segments of your uri before the routing takes place
rsegment(n) gives you the segments of your uri after routing (if routing occurred or the same as segment if it didn't)
so for /store/123456 getting rerouted to /store/index/123456
segment(1) == rsegment(1) == 'store'
rsegment(2) == 'index'
segment(2) == rsegment(3) == '123456'
Route:
$route['terms_and_conditions'] = 'SO_front/page/1';
controller :
public function page($id)
{
echo $id;
}
Output :
1
Here my solution for CI3...i would prefer laravel for advanced routing, orm,restapi, build in vuejs.
$route['store/(:any)'] = 'store/index/';
public function your index(){
$parameter=$this->uri->segment(2);
//use parameter for sql query.
}
Let say your route $route[store/product/update/(:any)] , then $parameter=$this->uri->segment(4)
Problem?. You will need to change entire file code if you plan to change the route name include view, controller, and custom route.