Symfony2: Problems with function parameter - php

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

Related

Get request from controller in model - Laravel

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);

Laravel fixed parameter value to controller from route

Can someone please tell me why it doesn't works? Returns nothing.
Route:
Route::get('/terms/privacy/', [
'uses'=>'contentController#dynamic',
'urlkey'=>'privacy'
])->name('privacy');
ContentController:
public function dynamic($urlkey){
return $urlkey;
}
You can create fixed routes in Laravel that specifies the parameters from the controller method using defaults. Like this
Route::get('/terms/privacy', ['uses'=>'contentController#dynamic'])->name('privacy')->defaults('urlkey', 'privacy');
You can look this
Alias for a route with a fixed parameter value
You can define the default value to the function paramter.
Route::get('terms/privacy', function ($urlkey = 'YourValue') {
return $urlkey;
});
//For Controller function
public function functionName($urlkey = 'test'){
return $urlkey;
}
And for the updated version you have default function also
Route::get('/terms/privacy', ['uses'=>'contentController#dynamic'])->name('privacy')->default('urlkey', 'privacy');

how can i use same route for two different controller function methods in laravel

how can i use same route for two different controller function methods in laravel
first controller
public function index()
{
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
second controller
public function index()
{
return view('search.index');
}
i want to use these two different controller functions for one route.
This is my route name
Route::get('/computer', [
'uses' => 'ComputerProductsController#index',
'as' => 'computer.list'
]);
laravel needs somehow to identify which exactly method you want. for example you can pass the parameter, which will identify which method to call.
public function index(Request $request)
{
// if param exists, call function from another controller
if($request->has('callAnotherMethod')){
return app('App\Http\Controllers\yourControllerHere')->index();
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}
You can't. If you want to add search functionality to your first controller's index page, you should determine which page to show inside your controller.
A possible example controller:
public function index(Illuminate\Http\Request $request)
{
// If the URL contains a 'search' parameter
// (eg. /computer?search=intel)
if ($request->has('search')) {
// Do some searching here and
// show the search results page
return view('search.index');
}
$comproducts = Comproduct::paginate(6);
$items = Item::orderBy('name')->get();
return view('computer', compact(['comproducts', 'items']));
}

PHP - Call function inside function

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?

Laravel - can I control routes by rule?

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

Categories