I need to pass a param like "link/page?url=true" or "link/page?url=false"
How can I set it in my route file?
Route::get('/link/page', 'TestController#index')->defaults('_config', [
'view' => 'test::home.page.index'
])->name('test.home.page.index');
If the param is going to be a query parameter, then there is no need to define it in the Route file. In the controller you can fetch the param as:
$request->query('url');
But if you really want the parameter to part of the URL itself then you can define it like this:
Route::get('/link/page/{url}', 'TestController#index')
and to fetch it in controller you have to do this:
TestController {
public function index($url){
//put your logic.
}
}
Related
I want to pass static value in route in laravel, is it possible in laravel ? here is what i have in route,
Route::post('manage-package', 'Api\App\HomeController#store');
I want to pass static value in this route, like params_one = 1, can we pass such parameter in route ? it will be great if anyone have something idea like this
One way to pass arbitrary data is to treat it like a route parameter by setting a default parameter on the route. This will cause that data to be passed to the route action as an argument just like if it was a route parameter:
Route::post('manage-package', 'Api\App\HomeController#store')
->defaults('params_one', 1);
public function store($params_one)
You could also use the 'actions' array of the Route but then would have to pull it from the route as opposed to having it passed like a parameter.
You can do that by defining an Optional Parameters in your route
Route::post('manage-package/{params_one?}', 'Api\App\HomeController#store');
And in the controller you define a default value of that parameter as the controller function receive each URL parameter as argument
public function store($params_one = 1){}
You can learn more about Optional Parameters
If our URL is http://127.0.0.1:8000/student/submit-details/1234 then its Route will be:
Route::get('student/submit-details/{id}',
'studentController#submitDetails')->name('submitDetails');
What will be the route if the URL is following?
http://127.0.0.1:8000/student/submit-details?code=1234
I'm using the following Route, but it is not picking it and not working. Does anyone know what will be its Route? I went through the documentation and found no help there.
Route::get('student/submit-details?code={id}', 'MyController#submitDetails');
Your route should be look like as:
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
http://127.0.0.1:8000/student/submit-details?code=1234
in above URL string after the question mark is query parameter and get the value of the query parameter in the controller you should use $_GET:
$_GET['code']
The placeholder parameters for routes are only specified for Route parameters but rather for query parameters. The Route should be only
Route::get('student/submit-details', 'MyController#submitDetails');
You can access the value in the controller via Request instance
public function submitDetails(Request $request) {
dd($request->code);
}
Try this
http://127.0.0.1:8000/student/submit-details?code=1234
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
You have to use get method
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
In Laravel if you want to pass data with GET method :
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
It will give you output like this :
http://127.0.0.1:8000/student/submit-details?code=1234
If you have multiple parameter, it will like :
http://127.0.0.1:8000/student/submit-details?code=1234&code2=5678
You can access the parameter from controller like this :
public function edit(Request $request){
$code = $request->input('code');
dd($code); // 1234
}
Take a look at the $_GET and $_REQUEST superglobals.
If you want route
http://127.0.0.1:8000/student/submit-details?code=1234
Route route will be
Route::get('student/submit-details', 'studentController#submitBankDetails')->name('submitBankDetails');
And usage
route('submitBankDetails', ['code' => 1234])
I want to call the method of B controller from Acontroller.So I used this method
return redirect()->action('UserController#subscribe()');
But here I am facing an issue here. I want to pass below param as in request body to above subscribe()
$package_id = session('package_id');
$package_type = session('package_type');
Please help me out how can I pass above param in request in laravel
If you need to call one controller from another, it seems like you have not so good architecture, and you have to refactor your code.
But, if you anyway want to do it, you can do it like that:
app('App\Http\Controllers\UserController')->subscribe($package_id, $package_type);
Controller 1
public function method1()
{
$package_id = session('package_id');
$package_type = session('package_type');
return app('App\Http\Controllers\Controller2')->method2($package_id, $package_type);
}
Controller 2
public function method2($package_id, $package_type)
{
//
}
You can append them to the query string.
return redirect()->action('UserController#subscribe', [
'package_id' => $package_id,
'package_type' => $package_type,
]);
You can call the controller.
app(UserController::class)->subscribe();
I use Laravel 5.7 for my JSON API web application.
In my routes/api.php file, I created the following route :
Route::apiResource('my_resource', 'API\Resource')->except(['delete']);
I added the corresponding controller and methods (index, show,...) and everythink work perfectly. My issue is the following : I would like to add optional GET params like this :
http://a.x.y.z/my_resource?param=hello¶m2=...
And for instance being able to retrieve 'hello' in my index() method. However, when I print the value of $request->input('param'), it's empty. I just don't get anything.
Yet, if I create a route like this, with an optional parameter:
Route::get('/my_resource/{param?}', 'API\Resource');
I'm able to get the parameter value in my controller method.
Here is my index method :
class Resource extends Controller {
public function index(Request $request)
{
print($request->input('param'));
// ...
}
// ...
}
Am I missing something ? I'm still new in Laravel maybe I missed something in the documentation.
Thanking you in advance,
You can use:
$request->route("param");
I would ilke to know if there is a way in Laravel 4 to pass parameters to filter from controller. I've seen many solutions for Laravel 3 but it seems this is working differently in L4. There is no 'filter' method. It is only beforeFilter. I tried passing params with 'action:param' method but no success :)
You can pass a parameter as a string after your filter name. Use : to separate method name and parameter.
public function __construct()
{
$this->beforeFilter('filterSomething:param');
}
If you have many parameters, you can use , to separate your parameters.
$this->beforeFilter('filterSomething:param1,param2');
And then define your filter logic in app/filters.php. Don't forget to add parameter in your filter method for passed parameters.
Route::filter('filterSomething', function($route, $request, $param1, $param2)
{
// Do something with your params.
});