Laravel Localization prefix - URl "/" not working without locale - laravel

I've did whats written in this post in order to add Localization prefixes to my URLs. However when I visit "/" there is an error: NotFoundHttpException in RouteCollection.php line 161:.
This is my Routesfile web.php:
Route::get('/', ['uses' => 'MainController#showMainPage', 'as' => 'showMainPage']);
Route::group(['prefix' => 'backend'], function () {
Route::get('/login', ['uses' => 'UserController#agentLogin', 'as' => 'agentLogin']);
});
Function:
class MainController extends Controller
{
public function showMainPage()
{
return redirect()->route('/fr');
}
}
localhost:8000/fr and localhost:8000/en are working fine.
How can I redirect / to the fallback locale (/fr)?

You can try make lang param optional:
'prefix' => '{lang?}'

Related

Laravel and routing

i have a larvel 8 project with other package and route and configured my route like this:
Route::group(['prefix' => config('site.route_prefix', 'site'), 'middleware' => config('site.route.middleware', 'web')], function () {
Route::get('/post/search/{q?}', [SearchController::class, 'search'])->name('site.search.query');
...
like this my search route not working, and the search method isnot handled.If i added some text to the route like this, then it work:
Route::group(['prefix' => config('site.route_prefix', 'site'), 'middleware' => config('site.route.middleware', 'web')], function () {
Route::get('/fdgfgfdgfdg/post/search/{q?}', [SearchController::class, 'search'])->name('site.search.query');
my question is where this behavior come from ?

nested resources in laravel not worked

I want to have nested resources like that:
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::resource('/articles', 'ArticleController');
Route::group(['prefix' => '/articles', 'as' => 'articles.'], function () {
Route::resource('/types', 'ArticleTypeController');
});
});
But nested route for "article/type" doesn't work, I check my ArticleTypeController outside the "article" route and work.
I really confused, Everybody can help me?
and here is my controller:
class ArticleTypeController extends Controller
{
public function index()
{
$types = ArticleType::all();
return view('manage.articles.types.index')->withtypes($types);
}
}
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::get('articles/types', 'ArticleTypeController#articleTypeMethod');
Route::resource('articles', 'ArticleController');
Route::resource('articles.types', 'ArticleTypeController');
});
for nested resources use articles.types. plural naming is good.
now that manage/articles and manage/articles/1/types will work.
If you want to put a custom route, put it above the resource route if the controller has been used as a resource. see the articles/types [GET] route which maps to ArticleTypeController's articleTypeMethod. now this way http://localhost.com/manage/articles/types should work
here is the 5.1 documentation and it has been removed from documentation of 5.5. but have a look at what Taylor said about it here
it's not recommended on REST to use index function for articles/types, a nested resources index method is used like articles/{id}/types.
for articles/types you need to create a new method.
but if you still want to do it like that. just make it like this
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::get('articles/types', 'ArticleTypeController#index');
Route::resource('articles', 'ArticleController');
Route::resource('articles.types', 'ArticleTypeController', ['except' => ['index']]);
});

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.

Multi domain routing in Laravel 5.2

I have setup multi-domain routing in my laravel 5.2 app. What I want to achieve is if a user hits, membership.app, he should be served different homepage as compared to user who hits, erp.app domain.
Route::pattern('erp', 'erp.app|erp.domain.com');
Route::pattern('membership', 'membership.app|membership.domain.com');
Route::group(['middleware' => ['web', 'auth'], 'domain' => '{erp}'], function() {
Route::get('/', 'HomeController#getIndex');
Route::controller('members', 'MembersController');
Route::controller('users', 'UsersController');
Route::controller('settings', 'SettingsController');
});
Route::group(['middleware' => 'web', 'domain' => '{erp}'], function () {
Route::controller('auth', 'Auth\AuthController');
});
Route::group(['middleware' => 'web', 'domain' => '{membership}'], function () {
Route::controller('/', 'BecomeMemberController');
});
Route::group(['middleware' => 'web'], function () {
Route::controller('ajax', 'AjaxController');
});
I tried this setup, but it breaks the code with first param in each controller method being the url instead of intended value.
Suppose I have a method hello in members controller.
public function hello($param1, $param2)
{
....
}
If I access erp.app/members/hello/1/2 url and try to print out $param1 of controller method, it returns erp.app instead of intended 1 in this case.
Please help.
I don't know why aren't you seperating the routes to different controllers as you say the output will be quite different...
A quick example of to use that:
Route::group(['domain' => '{type}.myapp.com'], function () {
Route::get('members/hello/{id1}/{id2}', function ($type, $id1, $id2) {
// when you enter --> members.myapp.com/hello/12/45
var_dump($type); //memebers
var_dump($id1); //12
var_dump($id2); //45
});
});

Controller in subfolder with namespace in laravel-4.1

I have the following code:
Route::group(array('namespace' => 'admin'), function() {
Route::group(array('prefix' => 'admin'), function() {
Route::get('group', array('as' => 'adminGroup', 'uses' => 'GroupController#index'));
Route::get('group/index', array('as' => 'adminGroupIndex',
'uses' => 'GroupController#index'));
});
});
and controller
namespace admin;
class GroupController extends \BaseController {
protected $layout = 'dashboard';
public function index()
{
$this->layout->content = \View::make('admin/group/index');
}
}
If I point the URL to:
http://localhost/laravel/public/admin/group/index
works perfectly, but when I point to:
http://localhost/laravel/public/admin/group
does not work. It just redirects to:
http://localhost/laravel/public/user/login
But when I do not use subfolder everything works perfectly!
EDIT: SOLVED
I had started installing laravel administrator and then stopped, because there was not installed an authentication system. So I installed Sentry2 and was configuring management groups. After analyzing a little more settings Laravel Administrator, I realized that it was using the URI 'admin' and also redirected to 'user / login' if I was not authenticated.
Now everything is working perfectly!
You probably have another route filtered with "auth" that is catching that /admin/group URL and sending it to login.
I just reproduced your code here and it works fine for me. For the sake of simplicity I just replaced my routes.php file with this code:
<?php
namespace admin;
class GroupController extends \Controller {
protected $layout = 'dashboard';
public function index()
{
return 'index!';
}
}
\Route::group(array('namespace' => 'admin'), function() {
\Route::group(array('prefix' => 'admin'), function() {
\Route::get('group', array('as' => 'adminGroup', 'uses' => 'GroupController#index'));
\Route::get('group/index', array('as' => 'adminGroupIndex',
'uses' => 'GroupController#index'));
});
});
And both
http://development.consultoriodigital.net/admin/group
http://development.consultoriodigital.net/admin/group/index
Worked fine showing a page with
index!

Resources