Laravel Redirect::action outputs extra "index" - laravel

I am using this code in a controller
Redirect::action('myController#getIndex')
and in routes.php
Route::controller('/my','myController');
I was expecting redirect to /my but this redirected to /my/index instead.
How can I fix the URL in this case.
P.S: Both URLs are ok and show same page, but I prefer the cleaner one.

With the ::controller routes the function name maps/plays a part in the url, so you would not be able to get around this easily using ::controller based routes. Instead I would advise to make use of named routes like so:
Route::get('my', array('as' => 'my.index', 'uses' => 'MyController#getIndex'));
Then to redirect to this route:
Redirect::route('my.index');
See this blog article post by Phil Sturgeon why using implicit routing is generally bad practise.

Related

Redirect to route results 'HTTP 500 Error' in laravel

I am developing a Laravel site. After a user registers and logs in, there are some jobs done for him and the the user should be redirected to a rout. Unfortunately, none of the redirections below produce a working page:
1: return Redirect::route('startChoose');
2: return redirect()->route('startChoose');
Below is the route that I've put in the web.php file:
Route::get('/startChoose', 'StartChooseController#index');
And this is the StartChooseController:
Would you please help me figure out what the problem can be? Thank you very much in advance.
You need to name your route to make route() helper build correct URL:
Route::get('/startChoose', 'StartChooseController#index')->name('startChoose');
Or:
Route::get('/startChoose', ['as' => 'startChoose', 'uses' => 'StartChooseController#index']);
If you don't want to name your route, use this syntax instead:
return redirect('startChoose');

How to declare routes with resources in Laravel 5.2

I have some routes in routes.php in laravel
// Code for rounting admin panel
Route::resource('/admin','Admin\LoginController#index');
Route::resource('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movies','Admin\MovieController#index');
Now when I access url http://localhost/askspidy/admin I want to show login page and it works, but when i access url http://localhost/askspidy/admin/dashboard it should go to dashboard but it's showing me login page only.
I know this is because when it found /admin in any url it's bydefault goes to the route
Route::resource('/admin','Admin\LoginController#index');
I know it's assuming that (/admin) is route to controller and (/dashboard) is the function declared in the controller but I want routing like this only so is there any other solution for this problem.
A RESTful Resource Controller takes over the responsibility of each action. You only need to list the name and the controller:
Route::resource('photo', 'PhotoController');
If you wanted to only use the index method, you’d list it like this:
Route::resource('photo', 'PhotoController', ['only' => [
'index'
]]);
However, it looks like two of your routes are not suitable for resources (login and dashboard), as they should relate to editing a model.
You should instead just use a get() resource instead.
From the docs:
Route::get('user/{id}', 'UserController#showProfile');
So in your case, it would be:
Route::get('/admin','Admin\LoginController#index');
Route::get('/admin/dashboard','Admin\AdminController#index');
Route::resource('/admin/movie','Admin\MovieController');

What does "as" keyword mean in Laravel routing?

As I understand from the Laravel documentation, it's used for redirection, but maybe I'm wrong.
I wrote
Route::get('user/profile', ['as' => 'profile', function () {
echo 'some_text';
}]);
then I was expecting my URL to redirect from
https://base_url/public/index.php/user/profile to https://base_url/public/index.php/profile but it doesn't happen.
Overall, I want to know, what the difference is if I used
Route::get('user/profile', function () {
echo 'some_text';
});
instead of the above routing rule.
The purpose isn't for re-direction in your routing file.
Instead, with the example route you provided, Laravel will allow you to reference said route by using:
$url = route('profile');
So you don't have to build the URL manually over and over again in your code.
So, in short: the difference is the first thing is a named route, and the last thing is a non-named route. Since you called the first route, you can reference it by that name.

how to pass variable from view to route in laravel

How to pass a variable from view to route in Laravel?
Here's the code at my route.php:
Route::get('/{id}/{id1}', 'WelcomeController#index');
And at welcome.blade.php:
< a href="{{URL::route('/{4}/{5}')}}">test</a>
I want to build a link referencing the route above.
A good approach to do it would be to name your route and then reference it by name, passing the parameters needed. Here is how:
At routes.php:
Route::get('/{id}/{id1}', ['as' => 'welcome_index', 'uses' => 'WelcomeController#index']);
And at your view, you can do this:
test
Notice that the first parameter represent the route name and the second, the parameters for your URL. You can read more here.
The advantages of naming a route is that you can change your route path later and your URL will still work with the code above.

How to check value of parameters of RESTful controller's action

I use the Larvel 4.1
If I use routes for every action I can check parameters value into route like here
Route::get('user/{id}', 'UserController#showProfile')->where('id', '[0-9]+');
But I don't understand how to check this value when I define a route for a RESTful controller
Route::controller('users', 'UserController');
Do I check parameters directly into controller's action or there is another way?
Yeah, looks like this is not the way Route::controller() works, but if you do
Route::pattern('one', '[0-9]+');
It will work. Because this is how Laravel create controller routes parameters:
GET|HEAD myactions/id/{one?}/{two?}/{three?}/{four?}/{five?}
The problem is that you cannot filter those parameters this way because they may appear in actions of different controllers. So you probably will have to create some of those routes manually:
Route::get('user/{id}', 'UserController#showProfile')->where('id', '[0-9]+');
Which, in my opinion is better than use a generic Route::controller() or Route::resource(). And in Phil Sturgeon's opinion too, as you can see in his article: http://philsturgeon.uk/blog/2013/07/beware-the-route-to-evil
So, I actually use separated routes for each action this manner
Route::pattern('one', '[0-9]+');
Route::get('user/{id}', 'UserController#showProfile');
I simple was hoped do it in more short way.

Resources