how to pass variable from view to route in laravel - 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.

Related

LARAVEL ROUTES not accept

and it works:
im add route and have view:
and errors:
wtf??? only copy paste, and rename...
Check layouts/app.blade.php There's something like route('qwe') and this route doesn't exist at all. So delete it from the layout and it should work.
Update
You're calling route('kwe') but you didn't name the route in the file. so add a name method to your route like the following.
Route::get('kwe', function(){
return view('kwe');
})->name('kwe');
Update 2
route helper's parameter is the route name not its path. So you need to name the route before calling it using route helper.
If you wanna use the path itself instead you can use url helper.

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

Laravel 5.2 blade route to resource

The documentation says:
If the named route defines parameters, you may pass the parameters as
the second argument to the route function. The given parameters will
automatically be inserted into the URL in their correct positions:
Route::get('user/{id}/profile', ['as' => 'profile', function ($id) {
//
}]);
$url = route('profile', ['id' => 1]);
If i do have a nested resource route by using Route::resource(...) twice, i will get a named route wich contains a placeholder like
employees.{employee}.images.index
How can i create a route for this nested resource using the blade templating engine?
I thought about
route('employees.{employee}.images.index', ['employee' => $employee->id]);
but that does not work. I know i can "manually" create the routes, but this will make them less maintainable.
Update 1
I know i can name the routes manually and then use the given name. But if there is a way without naming them i would prefer it.
You don't have to add anything like employee into a route name. Run php artisan route:list command and you will see real names of all routes (look at column called Name), created by resource clause. Then just use them like usual:
route('employees.images.index', ['employee' => $employee->id]);
Also, you can name resource routes.

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.

Laravel Redirect::action outputs extra "index"

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.

Resources