I am making a php framework, and i want to make the prefix set grouped. Like laravel's :
$router->group(['middleware'=>'auth', 'prefix'=>'test'], function (){
return $router->get("/","IndexController#index");
});
This is my Router
public function group($group = [], $callback){
// How To call the callback?
}
Or I Can't Do It?
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);
In my Laravel application I have created a controller named InvokableController. It contains this function:
public function __invoke()
{
return view('home');
}
I'd like to be able to call the __invoke method, like this:
Route::get('/', function () {
$invokableObj = new InvokableController();
$invokableObj();
})->name('home');
While this works:
Route::get('/', 'InvokableController')->name('home');
It's not what I'm looking for. I'd like to call the invoke method in this manner:
$invokableObj = new InvokableController();
$invokableObj();
I tried with and without the parentheses after InvokableController, on the line that assigns $invokableObj, with no luck.
I don't get any error messages. I tried returning something else at the bottom of the closure, like the number 1, and it renders 1 in the browser, so I know we're getting through the previous lines of code.
APP_DEBUG is true. Other error messages are rendered where I mess up. Does that make this some type of logical error?
Its just a class, so $invokableObj->__invoke() should work just fine. Or you can use laravel DI:
app()->call(InvokableController::class . '#__invoke');
Route::get('/', function () {
return app()->call(InvokableController::class . '#__invoke');
// or
$invokableObj = new InvokableController();
return $invokableObj->__invoke();
})->name('home');
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');
}
In my template I call a function like this:
loadResults('asc');
function loadResults(order) {
return $.get('{{ url('_example_results', { 'order' : ''}) }}'+order, function (html) {
$('#results').html(html);
});
}
The function in my controller looks like this:
public function resultsAction($order, Request $request)
{
// content is not crucial for solving my problem
}
My results don't get loaded, I get the following error:
Controller "...resultsAction()" requires that you provide a value for the "$order" argument (because there is no default value or because there is a non optional argument after this one).
What adjustments do I need to make?
Because TWIG render the page BEFORE you can act with js, you can't compose the right route with TWIG.
You can archive your problem with two approach:
1) Make the param optional and pass it on query string as follow:
js
loadResults('asc');
function loadResults(order) {
return $.get('{{ url('_example_results') }}'+"?order="order, function (html) {
$('#results').html(html);
});
}
controller
public function resultsAction(Request $request)
{
//...
$order= $request->get('order','asc'); // second parameter is the default if is null
}
2) Using FOSJsRoutingBundle
Hope this help
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