My route function in Laravel adds a question mark (?), instead of a slash (/)
route('servers.index', 321); // http://domain/public_html/server?321
I want it to return http://domain.com/public_html/clientarea/server/321
Routes:
Route::group(['prefix' => 'clientarea'], function()
{
Route::get('/', 'UsersController#index');
Route::get('server/{id}', 'ServersController#index');
});
Route::resource('users', 'UsersController');
Route::resource('servers', 'ServersController');
You should look into how the route() function is supposed to work. For example, you should name the route if you plan to reference it with route(). Once you have it named, you must make sure you're passing in the parameters correctly as Lukas said.
If Laravel can't find matching parameters defined for the route, it will default to making the paramaters a part of the query string. Since the route doesn't exist, it won't be able to find matching parameters to what you're passing in.
Take a look at the docs: http://laravel.com/docs/4.2/routing#named-routes
The route function expects an array for parameters. You can either pass the values by name of the parameter or by order
route('servers.index', array(321));
or this (assuming the parameter is called id
route('servers.index', array('id' => 321));
from the laravel forums
$url = URL::route('welcome') . '#hash';
return Redirect::to($url); // domain.com/welcome#hash
http://laravel.io/forum/02-07-2014-how-to-append-hashtag-to-end-of-url-with-redirect
Related
I am trying to pass the 2 parameters to my index method in the controller but it does not pass and says 0 have been sent. I have checked if the variables can be shown on the view which it can so that isn't the problem.
In my blade file I have the following <a tag:
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
web.php route:
Route::resource('attack', AttackController::class)->middleware('auth');
Controller:
public function index($debate, $argument){//}
Error message:
Too few arguments to function App\Http\Controllers\AttackController::index(), 0 passed in C:\Users\hakar\argupedia\Argupedia\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected
Blade File
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
Route File
Route::group(['middleware' => 'auth'], function () {
Route::get('/attack/{debate}/{argument}', 'AttackController#schemes')->name('search');
});
Controller
public function schemes($debate, $argument){
dd($debate, $argument);
}
If you're only using one method in a controller, there is no need for a resource route, Better way would be to create a single route like this:
Route::get('attack/{debate}/{argument}', 'AttackController#index')->middleware('auth')->name('attack.index');
The arguments in the curly brackets are called route parameters. Now, in order to pass this parameters, you need to pass them like key => value arrays
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
Now, you have access to these parameters in your controller:
public function index($debate, $argument){
dd($debate, $argument);
}
If both parameters are not required here, you can use optional parameters in your routes, by using ? mark, like this:
Route::get('attack/{debate?}/{argument?}', 'AttackController#index')->middleware('auth');
You can now pass only one parameter to your route, without crashing your application.
I got an error with redirecting I dont know why, i read the documentation of laravel 5.2 about redirecting and still not working pls help me thanks, here is my route in the routes.php
Route::get('/orders/view/{id}', 'OrderController#view');
here is my code in the controller for the redirecting
return redirect()->route('/orders/view/', ['id' => 5]);
and still it gives me this error
InvalidArgumentException in UrlGenerator.php line 314: Route
[/orders/view/] not defined.
The Problem
You're trying to redirect to a route by it's name, but you're passing an URL to the method.
return redirect() # Initialize a redirect...
->route(...); # to a route by it's name
The solutions
There are multiple ways to solve this. You can either redirect by path (that's
how you're trying to do at the moment), or by using route names.
Method 1: Redirect by path
You can redirect by path in two ways:
- concat the path by hand
- use the URL facade.
Concat the path by hand
$url = '/orders/view/' . $order->id;
return redirect($url);
Use the URL facade
# Remember to remove the trailing slash.
# Appending a trailing slash would lead to example.com/orders/view//1
$url = URL::to('/orders/view', [$order->id]);
return redirect($url);
You're done!
Method 2: Redirect by route name
To use route names, you need to prepare your routes and give them a name.
I recommend this way, because sometimes it's more clear but it also makes
maintaining your application easier when you need to change some paths, because
the names will be still the same. Read more about it here:
https://laravel.com/docs/5.2/routing#named-routes
This also allows you to use the easy-to-use and useful route() helper method.
Prepare your routes file
To assign your routes a name, you need to pass them to your router in your app/Http/routes.php.
# Example 1
Route::get('/orders/view/{id}', [
'as' => 'orders.view', 'uses' => 'OrderController#view'
]);
# Example 2
Route::get('/orders/view/{id}', 'OrderController#view')->name('orders.view');
For myself, I recommend to use the second syntax. It makes your code more
readable.
Redirect by route name
Now it's time to redirect! If you're using the method of route naming, you don't
even have to change much in your code.
return redirect()->route('orders.view', ['id' => $order->id]);
A note from my side
Have a look on route-model-binding in the documentation:
https://laravel.com/docs/5.2/routing#route-model-binding
This allows you to pass a model instance directly to the route() method and
it helps you fetching models by passing the proper model to your controller method
like this:
# Generate route URL
route('orders.view', $order);
# Controller
public function view(Order $order) {
return $order;
}
And in your app/Http/routes.php you would have {order} instead of {id}:
Route::get('/orders/view/{order}', 'OrderController#view');
You can use
return redirect()->action('OrderController#view', ['id' => 5]);
or
// $id = 5
return redirect()->to('/orders/view/'.$id);
I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?
I tried something like:
return redirect()->route('listofclubs');
It doesn't work.
The route for "listofclubs" is:
Route::get("listofclubs","Clubs#listofclubs");
If you want to use the route path you need to use the to method:
return redirect()->to('listofclubs');
If you want to use the route method you need to pass a route name, which means you need to add a name to the route definition. So if modify your route to have a name like so:
// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs#listofclubs']);
Then you can use:
return redirect()->route('listofclubs');
You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs#listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use
return redirect()->action('Clubs#listofclubs');
Then in route file
Route::get("listofclubs","Clubs#listofclubs");
Laravel will automatically redirect to /listofclubs.
I'm trying to create a route with an array of aliases, so when I call whois or who_is in the url it goes to the same route.
Then I don't need to keep repeating the code every time, changing only the alias.
I tried the code below.
Variables in the routes:
$path = 'App\Modules\Content\Controllers\ContentController#';
$aliases['whois'] = '(quemsomos|who_is|whois)';
Routes:
Route::get('{whois}', array('as' =>'whois', 'uses' => $path.'getWhois'))->where('whois', $aliases['whois']);
this one works as well
Route::get('{whois}', $path.'getWhois')->where('whois', $aliases['whois']);
Typing in the url my_laravel.com/whois or my_laravel.com/who_is or my_laravel.com/quemsomos will send me to $path.'getWhois' (which is correct).
But when I try to call it in the html on blade...
Who we are
The reference link goes to my_laravel.com//%7Bwhois%7D
How could I call route('whois') on my blade.php and make it work like when I type it on the url ?
I would like to use the route function` in my blade, so I can keep a pattern.
During route generation using the route function, Laravel expects you to set the value of the route parameter. You are leaving the parameter whois empty so the parameter capturing {whois} will not be replaced and results in the %7B and &7D for the accolades.
So in order to generate a route you will need to define what value you'd like to use for whois; {{ route('whois', ['whois'=>'whois']) }} for instance.
So i have checked out
PHP - Routing with Parameters in Laravel
and
Laravel 4 mandatory parameters error
However using what is said - I cannot seem to make a simple routing possible unless im not understanding how filter/get/parameters works.
So what I would like to do is have a route a URL of /display/2
where display is an action and the 2 is an id but I would like to restrict it to numbers only.
I thought
Route::get('displayproduct/(:num)','SiteController#display');
Route::get('/', 'SiteController#index');
class SiteController extends BaseController {
public function index()
{
return "i'm with index";
}
public function display($id)
{
return $id;
}
}
The problem is that it throws a 404
if i use
Route::get('displayproduct/{id}','SiteController#display');
it will pass the parameter however the URL can be display/ABC and it will pass the parameter.
I would like to restrict it to numbers only.
I also don't want it to be restful because index I would ideally would like to mix this controller with different actions.
Assuming you're using Laravel 4 you can't use (:num), you need to use a regular expression to filter.
Route::get('displayproduct/{id}','SiteController#display')->where('id', '[0-9]+');
You may also define global route patterns
Route::pattern('id', '\d+');
How/When this is helpful?
Suppose you have multiple Routes that require a parameter (lets say id):
Route::get('displayproduct/{id}','SiteController#display');
Route::get('editproduct/{id}','SiteController#edit');
And you know that in all cases an id has to be a digit(s).
Then simply setting a constrain on ALL id parameter across all routes is possible using Route patterns
Route::pattern('id', '\d+');
Doing the above will make sure that all Routes that accept id as a parameter will apply the constrain that id needs to be a digit(s).