Given the following Controller:
class Page {
public function about($section){
switch($section){}
}
}
How can I pass the a value to Page->about() directly from $f3->route?
Fat-Free will populate two parameters to each routing handler. So when you got this route:
$f3->route('GET /about/#section','\Page->about'); it will call your function with 1st parameter being the framework instance and 2nd is an array of all routing arguments.
class Page {
public function about($f3, $args){
switch($args['section']){}
}
}
See http://fatfreeframework.com/routing-engine#RoutesandTokens for more details.
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 have implemented a controller to give access of data of candidates.
class List_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$data[]=array();
/*
Here I want to get value of $area parameter, to save into logs
*/
$userId=$this->session->userdata('userId');
// save logs
$this->UrlAccessHistory->save($this->router->class,$this->router->method,$userId,"Web Application");
}
public function fetchList($area=null){
// fetch list of records from specified area
}
}
$this->router->className gives name of controller Class and $this->router->method gives name of of function called.
Please help to get the list of parameters at location (see comments in code) in the constructor, so that I can store it in the logs.
You could use the URI Class:
$this->uri->segment(n);
I have added a function inside controller to get list of controller parameters
function getControllerParametersFromUri($uri_string){
$list = explode("/",$uri_string);
// skip controller and method names from
// return array list
return array_slice($list,2);
}
// call function by passing uri_string to get parameters list
print_r($this->getControllerParametersFromUri($this->uri->uri_string()));
#Sujit Agarwal $this->uri->segment(3) also works,
but if we need list of all parameters then we may explode uri_string
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');
}
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'm using silex and I'm trying to use controllers as services. This conception works fine but I can't figure out how to pass arguments to controller method. Here is what I mean
IndexController.php
class IndexController
{
public function pagesAction($page)
{
return $page;
}
}
//app.php
$app['index.controller'] = $app->share(function() use ($app) {
return new Controllers\IndexController();
});
$app->get('/pages/{num}', "index.controller:pagesAction");
When I access pages/3 I get
Controller "SD\Controllers\IndexController::pagesAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
I also tried
$app->get('/pages/{num}', "index.controller:pagesAction:num");
Any ideas?
Change this
class IndexController
{
public function pagesAction($page) //what is $page? Is not into route
{
return $page;
}
}
to
class IndexController
{
public function pagesAction($num)
{
return $page;
}
}
This is because silex (and is also Symfony2 logic, of course) expects arguments name to be exactly the same from route to controller
OR
you should change your route to be parametrized for $page variable