i have 2 routes with POST methods
Route::post('/payment/checkOrder','Finance\PaymentCallbackController#checkOrder');
Route::post('/payment/paymentAviso', 'Finance\PaymentCallbackController#paymentAviso');
how can i create legacy links for these routes?
/plat.php?paysystem=5&method=checkOrder
/plat.php?paysystem=5&method=paymentAviso
You can have a single route that recieves a method string, and then call the desired functions according to it.
Route::post('/payment/{method}','Finance\PaymentCallbackController#handler');
// PaymentCallbackController.php
public function handler(Request $request){
// make sure to validate what methods get sent here
$this->{$request->method}($request);
// use $this if its in this controller, for otherControllers
// try something with the looks of app('App\Http\Controllers\OtherControllerController')->{$request->method}->($request);
}
Add this route:
Route::post('/plat.php', 'SomeController#action');
In your controller function:
// SomeController.php
public function someAction()
{
$paysystem = $request->query('paysystem');
$method = $request->query('method');
// some logic here
return view('something');
}
Related
In my API I used "with" method to get parent's model relation and everything works fine.
I want to add an attribute in my relation and return it in my API but I should use request in my model.
Something like this :
Book.php
protected $appends = ['userState'];
public function getUserStateAttribute () {
return User::find($request->id); //request not exists here currently
}
I have $request in my controller (api controller)
Controller.php
public function get(Request $request) {
Post::with('books')->all();
}
I believe using static content to append in array of model is so easy but how about using request's based content ?
I guess you can use request() helper :
public function getUserStateAttribute () {
return User::find(request()->get('id'));
}
Sure this is not really MVC pattern, but it can work
You want to take request as a parameter here:
public function getUserStateAttribute (Request $request) {
return User::find($request->id);
}
That way you can access it in the function. You will just need to always pass the Request object whenever you call that function.
e.g. $book->getUserStateAttribute($request);
Alternatively, you could just pass the ID, that way you need not always pass a request, like so:
public function getUserStateAttribute ($id) {
return User::find($id);
}
Which you would call like:
e.g. $book->getUserStateAttribute($request->id);
I would like create one route with index method and all method name if exist can be call. Is any way to do this ?
Route::get('/{params}/table', TableController#index)->name(table.index)
Now in controller i would like create index method and all request should go there. I want get params as method and try call if exist.
public function index(Request $request) {
dd($request);
}
How can i do this ?
UPDATE
I think i need smth like:
Route::get('/{params}/abc/{any?}, function(any) {
get params $any
call function from TableController#any
});
and if method exist in TableController
You can simple receive $params or $any variable in the controller
public function index($params, $any) {
dd($any);
}
and than check if needed method exists.
My Code in route.php:-
Route::get('/register/{id}',array('uses'=>'UserRegistration#id));
I want to call function id (that can be any function of controller) in UserRegistration controller.
Url is like this:- http://localhost:8000/register/test,
http://localhost:8000/register/login
here test and login are function in controller.
{id} is the parameter you're passing to route. So, for your routes go with something like this:
Route::get('/register/id/{id}',array('uses'=>'UserRegistration#id));
//this route requires an id parameter
Route::get('/register/test',['uses'=>'UserRegistration#test]);
Doing this you can call your functions but it is not the recomended way of doing it. Having separete routes foreach function allows for a more in depth control.
public function id(Request $request)
{
return $this->{$request->id}($request);
}
public function test(Request $request)
{
return $request->all();
}
I have a route defined which calls a test function in TestController.
Route::get('/test/{function_name}','TestController#test');
This test function calls internally the function that matches the name inside the TestController.
This works for the functions that doesn't need paramters. However certain functions needs paramters and then the route becomes invalid.
public function test($function_name)
{
try
{
var_dump($this->$function_name());
return;
}
catch(Exception $ex)
{
throw $ex;
}
}
// This functions get called fine
public function getRecord(){}
// But this functions does not work because i am passing extra paramters in the url which in turns makes the route invalid
public function getRecordByNameAndPrice($name, $price){}
So is there any way that i can defined a route in such a way that it should contain 1 parameter but should also allow N number of extra parameters so that i can call those functions that needs paramters.
Thanks
Use the where method to allow your rest to contain slashes:
Route::get('test/{func}/{rest?}', 'TestController#test')->where('rest', '.*');
Then use $request->segments() to get them all as separate values:
public function test($method, Request $request)
{
$params = array_slice($request->segments(), 2);
return call_user_func_array([$this, $method], $params);
}
Don't forget to use Illuminate\Http\Request up top.
let's say your url is
/products?id=999&format=json&apikey=123456
and define your route like this
Route::get('/prodcuts',function(){
return Request::all();
})
// output
{"id":"999","format":"json","apikey"=>"123456"}
So I have a Laravel Controller (MainController.php) with the following lines:
...
public function _settings_a(){
return view('_settings_a');
}
public function _settings_b(){
return view('_settings_b');
}
public function _settings_c(){
return view('_settings_c');
}
public function _settings_d(){
return view('_settings_d');
}
public function _staff_a(){
return view('_staff_a');
}
public function _staff_b(){
return view('_staff_b');
}
public function _staff_c(){
return view('_staff_c');
}
...
And my routes.php is as follows:
Route::any('_staff_a''MainController#_staff_a');
Route::any('_staff_b''MainController#_staff_b');
...
etc.
It seems there are a LOT of lines and a LOT of things to change if I change my mind...
I was wondering if I can have some regex in routes.php and an equivalent regex in MainController.php for handling routes that begin with an underscore (_)?
Can any Laravel experts share some tips/suggestions? I'm quite new to the framework.
Sure - just add it as a parameter. E.g. like this:
Route::any('_staff_{version}', 'MainController#_staff');
public function _staff($version) {
return view('_staff_'.$version);
}
I don't think you need to mess with regex. You can use implicit controllers Route::controller() which isn't the BEST solution, but will do what I think you are wanting.
So instead of
Route::any(..)
you can do
Route::controller('url', 'MainController');
So your route to whatever 'url' is will send you to this controller. Follow that with a '/' and then add whichever method in the controller you want to call.
Here is an example:
My url: 'http://www.example.com/users'
// routes.php
Route::controller('users', UserController');
// UserController.php
public function getIndex()
{
// index stuff
}
Now I send a request like: http://www.example.com/users/edit-user/125
// UserController.php
public function getEditUser($user_id)
{
// getEditUser, postEditUser, anyEditUser can be called on /users/edit-user
// and 125 is the parameter pasted to it
}
Doing it this way should allow you to be able to just send a request (post or get) to a url and the controller should be able to call the correct method depending on the url.
Here are some more rules about it: http://laravel.com/docs/5.1/controllers#implicit-controllers