Laravel 4 : Default route for route group - laravel

I try to handle the default route of a route group, I have this but it doesn't work.
Route::group(array('prefix' => 'administrator'), function() {
Route::get('/', 'AdminUserController#getLogin');
Route::controller('page', 'AdminPageController');
Route::controller('user', 'AdminUserController');
Route::controller('menu', 'AdminMenuController');
});
Does anyone know how to do that ?
Thank you

Just figured out. You are missing a uses, and the second parameter should be an array.
Route::get('/', ['uses' => 'AdminUserController#getLogin']);

Related

How Can i Call Middleware in this prefix Route?

Hello I am new in laravel framework. can anyone tell me how to apply middleware in this following route?
Route::prefix('Admin')->group(function (){
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
});
There are various to call middleware in the group function.
1st way:- Define middleware after group function.
Route::prefix('Admin')->group(function (){
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
})->middleware('yourmiddlewarename');
2nd way:- to define middleware with a prefix.
Route::middleware(['yourmiddlewarename'])->prefix('Admin')->group(function (){
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
});
You should use Laravel's Route::group() method for proper grouping of routes.
You can group routes like the following:
Route::group(['as' => 'for_named_route','prefix' =>'for_prefixing','namespace' => 'for_namespacing', 'middleware' => 'for_middleware'],function(){
// Your route will go here
);
For your coding purpose your route group should be like the following:
Route::group(['prefix'=>'for_prefixing','middleware'=>'for_middleware'],function(){
// Your route will go here
Route::get('/', 'UserlistController#index');
Route::post('create', 'UserlistController#create')->name('create');
);
You can also pass multiple middleware using an array like:
'middleware'=>['middleware_1','middleware_2']
Route::group(['prefix'=>'admin','middleware'=>['auth']], function(){
Route::post('favorite/{post}/add','FavoriteController#add')->name('post.favorite');
Route::post('review/{id}/add','ReviewController#review')->name('review');
Route::get('file-download/{id}', 'PostController#downloadproject')->name('project.download');
Route::post('file-download/{id}', 'PostController#downloadproject');
});

why i see Call to undefined method Error?

im learning laravel so if im not well as you are accept my apologies...
my problem is when i try to define a new method in web.php i got error!some times phpstorm sets problem on 'Route'word so i can run my blade pages but sometimes it sets problem on 'get','post','group' ,...and i cant run my app
ill show you how i defined my routes
use Illuminate\Routing\Route;
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin'] ,function (){
Route::get('/users','UsersController#index');
});
after all this my point is making a controller so i got this error to fix so i can move on...
Named groups in laravel
Route::group(['prefix'=>'admins','as'=>'admin.'], function(){
Route::get('users', ['as' => 'user', 'uses' = > 'UsersController#index']);
});
Also make sure you have index method in your UsersController.
FYR :- https://laraveldaily.com/laravel-5-1-names-for-route-groups/

Use different route namespace based on middleware in Laravel

I have the following code in my routes/web.php
Route::namespace('Admin')->middleware(['admin'])->group(function() {
Route::get('/posts', 'PostController#index');
});
Route::namespace('User')->middleware(['user'])->group(function() {
Route::get('/posts', 'PostController#index');
});
I wish to use the same uri "/posts" in both cases and keep the role logic (admin, user) out of the controllers, however, in this case, when I request the route "/posts" in always responds with the last one.
I can't seem to find information of what I am missing here.
use prefix for different route for admin and user
/admin/posts
Route::group(['namespace' => 'Admin','middleware=>'admin','prefix' => 'admin'],function() {
Route::get('/posts', 'PostController#index');
});
/user/posts
Route::group(['namespace' => 'User','middleware=>'user','prefix' => 'user'],function() {
Route::get('/posts', 'PostController#index');
});
You may try this one
Route::group(['prefix'=>'admin','middleware'=>'admin'],function (){
Route::get('/posts',['uses'=>' PostController#posts','as'=>'posts.index']);
});
Route::group(['prefix'=>'user','middleware'=>'user'],function (){
Route::get('/index',['uses'=>' PostController#posts','as'=>'posts.index']);
});

How to add dynamically prefix to routes?

In session i set default language code for example de. And now i want that in link i have something like this: www.something.com/de/something.
Problem is that i cant access session in routes. Any suggestion how can i do this?
$langs = Languages::getLangCode();
if (in_array($lang, $langs)) {
Session::put('locale', $lang);
return redirect::back();
}
return;
Route::get('blog/articles', 'StandardUser\UserBlogController#AllArticles');
So i need to pass to route as prefix this locale session.
If you want to generate a link to your routes with the code of the current language, then you need to create routes group with a dynamic prefix like this:
Example in Laravel 5.7:
Route::prefix(app()->getLocale())->group(function () {
Route::get('/', function () {
return route('index');
})->name('index');
Route::get('/post/{id}', function ($id) {
return route('post', ['id' => $id]);
})->name('post');
});
When you use named routes, URLs to route with current language code will be automatically generated.
Example links:
http://website.com/en/
http://website.com/en/post/16
Note: Instead of laravel app()->getLocale() method you can use your own Languages::getLangCode() method.
If you have more questions about this topic then let me know about it.
Maybe
Route::group([
'prefix' => Languages::getLangCode()
], function () {
Route::get('/', ['as' => 'main', 'uses' => 'IndexController#index']);
});

Laravel router redirect to wrong path

I wrote this code expecting to be redirected to the root url (/ or /home), but instead, it's redirecting to %7Bhome%7D.
Route::get('/{home}', ['as' => 'home', function () {
return view('home');
}])->where('home', '(home)?');
Does anyone know where the problem is?
note: it does support both urls (/ or /home). the problem is just when calling for its name on redirects.
Thanks in advance!
One solution is just to create two routes with the two URLs:
Route::get('/', 'yourController#methodName');
and the other one:
Route::get('/home', 'yourController#methodName');
As you might know, the %7B and %7D are referring to the left and right curly braces { }. I'm not sure why, but it's obviously not leaving out the curly braces when it tries to direct to your route .
It's strange that You cannot redirect using named routes.
Give a try to this:
Route::get('{location}', ['as' => 'home', function ($location = 'home') {
return view($location);
}])->where('location', '(home)?');
or how about this:
Route::any('/', ['as' => 'home', function () {
return view('home');
}]);

Resources