Facing an issue with routing - Laravel - laravel

I started working with Laravel last week and I'm facing a minor problem with routes.
when i do the following:
Route::group(array('before' => 'auth'), function() {
Route::resource('admin', 'VacatureController');
Route::get('admin/test', array('uses' => 'VacatureController#create'));
Route::post('admin/test', array('uses' => 'VacatureController#store'));
});
and i go to admin/test, i get an empty page.
when i change admin/test to something like test/test like:
Route::group(array('before' => 'auth'), function() {
Route::resource('admin', 'VacatureController');
Route::get('test/test', array('uses' => 'VacatureController#create'));
Route::post('test/test', array('uses' => 'VacatureController#store'));
});
it works fine. I looked itup in the documentation, but i didn't become anything wiser.
Can someone please enlighten me?

Try putting the Route::resource as the last route. Laravel will try all routes in the order you put them in the route file, so when you put the resource route first only this one will be checked because it expects all admin routes to be there.
Route::group(array('before' => 'auth'), function() {
Route::get('admin/test', array('uses' => 'VacatureController#create'));
Route::post('admin/test', array('uses' => 'VacatureController#store'));
Route::resource('admin', 'VacatureController');
});

Related

laravel 5.2 make dashboard for admin

I am new in laravel 5.2. I want to make dashboard for admin but i do not understand how to make it. I did copy all controller files in admin folder and also copied view folder in admin folder.
I did try some code which is mention below:-
Route::group(array('namespace'=>'Admin'), function()
{
Route::get('/admin', array('as' => 'admin', 'uses' => 'UserController#index'));
Route::get('/admin/register', array('as' => 'register', 'uses' => 'UserController#register'));
Route::get('/admin/login', array('as' => 'login', 'uses' => 'UserController#login'));
});
but now I want to show all controller files under admin like:-
localhost:8000/admin/users
If you want to use all route of admin on localhost:8000/admin.
You should use route group and add prefix on it, like that
Route::group(['prefix' => 'admin/'], function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
Route::get('login', function () {
// Matches The "/admin/login" URL
});
.......
});
Read more at this doc https://laravel.com/docs/5.2/routing#route-groups
I hope this could help you.

DaveJamesMiller Breadcrumbs Error - Laravel

I am currently trying to set up breadcrumbs for my Laravel 5 application. Unfortunately, I am currently being presented with this error when I access localhost:8888/auth/login:
ErrorException in
/Users/ben/Sites/laravel/vendor/davejamesmiller/laravel-breadcrumbs/src/CurrentRoute.php
line 29
The current route (GET /auth/login) is not named - please check
routes.php for an "as" parameter
Routes.php:
Route::get('auth/login', 'Auth\AuthController#getLogin',
['as' => 'login', 'uses' => 'Auth/AuthController#getLogin']);
The error is shown with or without the ['as' => 'login', 'uses' => 'Auth/AuthController#getLogin'] addition.
Breadcrumbs.php
Breadcrumbs::register('login', function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('Login', route('login'));
});
Thank you for your help.
I resolved this issue by changing the route to the following:
Route::get('auth/login',
['as' => 'login', 'uses' => 'Auth/AuthController#getLogin']);
You can only declare which controller method you're using once.

Display home page in base url without showing /home (Laravel)

If I type my website url in browser as www.test.com/ then it redirects to www.test.com/home. But I want to see the url in browser as www.test.com only.
routes.php
Route::get('/', array('uses' => 'HomeController#home', 'as' => 'home'));
Route::get('home', array('uses' => 'HomeController#home', 'as' => 'home'));
Try this in your route file:
Route::get('/', 'Admin\HomeController#index');
and remove the extra home route.
See, if that helps.
Route::get('/', array('uses' => 'HomeController#home', 'as' => 'home'));
Keep this route only and remove the next route
It will work as you are expecting then

Routing confusion in laravel 4

I'm experiencing routing confusion in laravel 4.
Route::group(['prefix' => 'myProfile', 'before' => 'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
When i write to address bar domain.app/myProfile it runs second route and runs ProfileController#index...
Thanks.
Looks like correct behaviour. To access first route you would have to type something like domain.app/myProfile/FooUser. You didn't specify / route in myProfile route group, so it cannot match it and uses second one.
Breaking down your routes:
1)
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
Use /example URI to access the above route.
2)
Route::group(['prefix' => 'myProfile', 'before' =>'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Use /myProfile/example URI to access the above route.
Your application is working as expected.

Laravel Routes Artisan

When I run artisan routes in laravel 4
auth/login/{v1}/{v2}/{v3}/{v4}/{v5}
Is this normal or is there something wrong. My routes work just wondering if there might be a bug or something. Below are my routes for auth. I'm using restful routes for auth.
Route::controller('auth','AuthController');
Route::get('AuthController/login', array('as' => 'login', 'uses' => 'AuthController#login'));
Route::get('auth/logout', array('as' => 'logout', 'uses' => 'auth#logout'));
Route::post('auth/login', array('uses' => 'auth#login'));
This is expected. When you register controllers with Route::controller() the controller inspector adds the URI wildcards. Consider the following example:
Route::controller('user', 'UserController');
You might then have a method like this on your UserController:
public function getProfile($username)
{
$user = User::where('username', $username)->first();
return View::make('profile')->with('user', $user);
}
You could then hit that method by going to localhost/yourapp/user/profile/jason
In a nut shell it allows you to pass extra parameters to a method. To me this is a very old school way of doing it, as it looks nicer like localhost/yourapp/user/jason/profile and in this case you'd need to use a route to map to the controller method.
I suggest you 2 improvements:
1 - keep a standard with URIs
You don't need Route::controller in this case. In order to mantain all routes with the same structure I would do:
Route::group( array('prefix'=>'auth,function(){ //make all auth routes starting by auth
Route::get('getLogin', array('as' => 'getLogin', 'uses' => 'AuthController#getLogin'));
Route::get('getLogin', array('as' => 'logout', 'uses' => 'AuthController#logout'));
Route::post('postLogin', array('as' => 'postLogin', 'uses' => 'AuthController#postLogin'));
});
It is not necessary to use group but if you app grows could be better. Without group code will be:
Route::get('auth/getLogin', array('as' => 'getLogin', 'uses' => 'AuthController#getLogin'));
Route::get('auth/getLogin', array('as' => 'logout', 'uses' => 'AuthController#logout'));
Route::post('auth/postLogin', array('as' => 'postLogin', 'uses' => 'AuthController#postLogin'));
2 - Protect your post routes
For every post and put request we've to prevent CSRF attacks like this:
Route::post('postLogin',array('before' => 'csrf','uses'=>AuthController#postLogin) );

Resources