I have admin prefix where url/admin/dashboard is my dashboard view.
What I need is to redirect users to url above if they type only url/admin .
This is what I have:
Route::prefix('admin')->group(function () {
Route::get('dashboard', 'HomeController#index')->name('dashboard'); //works
Route::get('/', function () {
return redirect()->route('dashboard');
}); //doesn't work
});
You might want to use this:
Route::get('url/admin/dashboard', 'HomeController#index')->name('dashboard');
Route::get('url/admin', function () {
return redirect('url/admin/dashboard');
});
You can do
Route::get('url/admin/{name?}', 'HomeController#index')
->where('name', 'dashboard')
->name('dashboard');
Or if you want to use the prefix
Route::prefix('admin')->group(function () {
Route::get('/{name?}', 'HomeController#index')
->where('name', 'dashboard')
->name('dashboard');
});
The latest Laravel made it even easier. Define the route for dashboard followed by redirect. Have a look.
Route::get('url/admin/dashboard', 'HomeController#index')->name('dashboard');
Route::redirect('url/admin', 'url/admin/dashboard');
Related
I'm a beginner with laravel and trying to figure out the routes. I searched the web but provided solutions don't help, so maybe you guys will be able to shed some light.
So below is my routes file (web.php)
Route::group(['middleware' => 'auth'], function () {
Route::get('/', [HomeController::class, 'home']);
Route::get('dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::get('billing', function () {
return view('billing');
})->name('billing');
Route::get('profile', function () {
return view('profile');
})->name('profile');
Route::get('rtl', function () {
return view('rtl');
})->name('rtl');
Route::get('user-management', function () {
return view('laravel-examples/user-management');
})->name('user-management');
Route::get('tables', function () {
return view('tables');
})->name('tables');
Route::get('virtual-reality', function () {
return view('virtual-reality');
})->name('virtual-reality');
Route::get('static-sign-in', function () {
return view('static-sign-in');
})->name('sign-in');
Route::get('static-sign-up', function () {
return view('static-sign-up');
})->name('sign-up');
Route::get('/logout', [SessionsController::class, 'destroy']);
Route::get('/user-profile', [InfoUserController::class, 'create']);
Route::post('/user-profile', [InfoUserController::class, 'store']);
Route::get('/login', function () {
return view('dashboard');
})->name('sign-up');
Route::get('/register', [RegisterController::class, 'create']);
Route::get('/user-list', [RegisterController::class, 'index'])->name('user-list');
Route::post('/register', [RegisterController::class, 'store']);
//Route::delete('user-destroy', [RegisterController::class, 'destroy'])->name('user-destroy');
Route::resource('users', RegisterController::class);
//Route::get('/viehicle-types', [ViehicleTypeController::class, 'index']);
// Route::resource('viehicle-types', ViehicleTypeController::class);
Route::group(['middleware' => ['role:admin|manager']], function () {
Route::resource('viehicle-types', ViehicleTypeController::class);
Route::resource('addon-types', AddonTypeController::class);
Route::resource('addons', AddonController::class);
Route::resource('job-types', JobTypeController::class);
Route::resource('jobs', JobController::class);
Route::resource('viehicles', ViehicleController::class);
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
Route::resource('finished-jobs', FinishedJobController::class);
});
});
My problem is that some of the name routes won't work.
Mainly these 2 don't work and display a 404 Error:
Route::get('/finished-jobs/registerSelectJobType', [FinishedJobController::class, 'registerSelectJobType'])->name('xxxx');
Route::get('/finished-jobs/registerSelectJob/{jobTypeId}', [FinishedJobController::class, 'registerSelectJob'])->name('yyyy');
What is strange that when I check the routes:
$url=route('xxxx');
echo "The url is : " .$url;
I get the proper URL that works when entered in the web browser.
So I can't figure it out if anyone can help I will be greatful.
Cheers
HT
I have a project which have multiple subdomains.
for example I have a subdomain for Students which goes to a student controller and it looks like this:
Route::domain('students.domain.test')->group(function () {
Route::get('/', function () {
return "done reaching the students page";
});
});
The second type of domains is "domain.test" and any subdomain which I'm checking in the request level and that's fine too.
Route::get('/', [HomeController::class, 'index'])->name('index');
But before the second type of domains I want to make subdomain for specific types of Entities which I have in the database.
Route::domain('{someTypes}.domain.test')
->group(function () {
Route::get('/', function () {
return "done reaching SomeTypes Page";
});
});
My Entity table have these attributes: Id, Title, Type "which I want to check if the type is 5".
I tried to use the middleware:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
return $next($request);
}
abort(404, 'Sorry, Request Not Found');
}
and I applied it to my routes like this:
Route::group([
'middleware' => ['type:journal']
],function () {
Route::get('/', function(){
return 'journals logic goes here';
});
});
and I have another middleware to ignore types like this:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
abort(404, 'Sorry, Request Not Found');
}
return $next($request);
}
and applied it to the other routes like this:
Route::group([
'middleware' => ['except_entity:journal']
], function(){
Route::get('/', function(){
return 'default pages when journals fails';
})->name('index');
I hope its clear what I'm trying to achieve.
First, you need a check what version laravel that you used?
You need to use Middleware. And I think, method to code with laravel 6, 7, or 8, is a little bit different.
Can you give us more information about your code, so we can help it easier?
I’m currently working on a new website and want to use multi language on different pages and the URL should include the prefix.
This is my goal and should be achieved:
Mywebsite/ --> Mywebsite/en
Mywebsite/en --> Should show the main page in en
Mywebsite/de --> Should show the main pahe in de
Mywebsite/en/legal --> Should show legal page in en
Mywebsite/legal --> Should show the legal page in the before selected language and redirect to Mywebsite/en/legal
So if the prefix is missing the site should be redirected and show the exact page in the correct language.
I tried to implement this way but not all cases are working.
web.php
Route::get('/', function () {
return redirect(app()->getLocale());
});
Route::get('/legal', function () {
return redirect(app()->getLocale() . '/legal');
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'localization'], function($locale) {
Route::get('/', function () {
return view('home');
});
Route::get('/home', function () {
return view('home');
});
Route::get('/legal', function () {
return view('legal');
});
});
Localization.php
public function handle($request, Closure $next)
{
app()->setLocale($request->segment(1));
return $next($request);
}
app()->getLocale() does not work at all
Route::get('/legal', function () is also not working
What is missing or what can I change to solve it?
Thanks in advance!
the controller is returning a blank data/view and I think something is wrong with my routes. if I remove {locale}, the data is retrieved.
Can anyone help with returning the data properly while my routes have {locale} in it? Here are my related code:
Web.php
Route::get('{locale}/projects/{id}/billings', 'ProjectController#showbilling')
->name('showbilling');
Route::post('{locale}/projects/{id}', 'ProjectController#addbilling')
->name('addbilling');
ProjectController.php
public function showbilling($id)
{
$billings = Project::find($id);
$locale = app()->getLocale();
return $billings;
//return view('admin.addbillings', compact('billings'));
}
Edit: Here's my full web.php
web.php
Route::get('/', function() {
return redirect(app()->getLocale());
});
Route::group(['prefix' => '{locale}', 'where' => ['locale' => '[a-zA-Z]{2}'], 'middleware' => 'setlocale'], function () {
Route::get('/', function () {
return view('welcome');
})->name('main');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
//Customers
Route::get('/customers', 'CustomerController#showcust')->name('customers');
Route::post('/sendcust', 'CustomerController#sendcust')->name('sendcust');
//Items
Route::get('/items', 'ItemController#showitems')->name('items');
Route::post('/senditem', 'ItemController#senditem')->name('senditem');
//Projects
Route::get('/projects', 'ProjectController#showprojects')->name('projects');
Route::post('/sendproj', 'ProjectController#sendproj')->name('sendproj');
//ProjectBillings
Route::get('/projects/{id}/billings', 'ProjectController#showbilling')->name('showbilling');
Route::post('/projects/{id}', 'ProjectController#addbilling')->name('addbilling');
//Invoices
Route::get('/invoices', 'InvoiceController#showinvoice')->name('invoices');
Route::post('/sendinvoitem', 'InvoiceController#sendinvoitem')->name('sendinvoitem');
Route::get('/invoices/{id}/details', 'InvoiceController#showdetails');
Route::post('/updateitem','InvoiceController#updatedetail')->name('updateitem');
Route::get('invoices/{id}/generate', 'InvoiceController#generate');
Route::post('/updatestatus', 'InvoiceController#changestatus')->name('updatestatus');
});
You are passing 2 params in your route but accepting only 1 in the controller. Add locale.
public function showbilling($locale, $id)
I need all the route under same prefix manager with one middleware for guest manager_guest and another for logged in user manager_auth.
This code bellow is my route web.php file.
Is there any other way ?
My routes:
Route::prefix('manager')->group(['middleware' => 'manager_guest'], function () {
Route::get('/register', 'Manager\RegisterController#showRegister')->name('manager.register.create');
Route::post('/register', 'Manager\RegisterController#register')->name('manager.register.store');
Route::get('/login', 'Manager\LoginController#showLogin')->name('manager.login.create');
Route::post('/login', 'Manager\LoginController#login')->name('manager.login');
});
Route::prefix('manager')->group(['middleware' => 'manager_auth'], function () {
Route::post('/logout', 'Manager\LoginController#logout')->name('manager.logout');
Route::get('/profile', 'Manager\PageController#profile')->name('manager.profile');
});
Error after executing php artisan route:list
PHP Warning: Uncaught ErrorException: Array to string conversion in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php:329
Stack trace:
#0 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Array to string...', 'E:\\laragon\\www\\...', 3
29, Array)
#1 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require()
#2 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(285): Illuminate\Routing\Router->loadRoutes(Array)
#3 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\RouteRegistrar.php(104): Illuminate\Routing\Router->group(Array, Array)
#4 E:\laragon\www\laraveladmin\routes\web.php(30): Illuminate\Routing\RouteRegistrar->group(Array, Object(Closure))
#5 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require('E:\\laragon\\www\\...')
#6 in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php on line 329
PHP Fatal error: Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR') in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate
\Routing\Router.php on line 329
[Symfony\Component\Debug\Exception\FatalErrorException] Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR')
Try this instead
Route::group(['prefix' => 'manager', 'middleware' => 'manager_guest'], function() {
});
You could "factorize" your code like this:
Route::prefix('manager')->group(function () {
Route::middleware(['manager_guest'])->group(function () {
// These will be prefixed with "manager" and assigned the "manager_guest" middleware
});
Route::middleware(['manager_auth'])->group(function () {
// These will be prefixed with "manager" and assigned the "manager_auth" middleware
});
// These will just be prefixed with "manager"
});
I noticed all your controllers live in the sub-namespace Manager. You can chain the methods and make your routes file even cleaner. For instance:
Route::prefix('manager')->namespace('Manager')->group(function () {
Route::middleware(['manager_guest'])->group(function () {
Route::get('register', 'RegisterController#showRegister')->name('mananger.register.create');
});
Route::middleware(['manager_auth'])->group(function () {
Route::get('profile', 'PageController#profile')->name('mananger.profile');
});
});
None of the other answers worked for me as I had a lot of routes to change, and didn't want to change namespaces. The key to making this work is "as". The downside of this being that it changes the path when using "route()", but your use of name on each route here would override that anyway.
Route::group(['prefix' => 'manager', 'middleware' => ['manager_guest'], 'as' => 'manager_guest'], function() {
...
}
Route::group(['prefix' => 'manager', 'middleware' => ['manager_auth'], 'as' => 'manager_auth'], function() {
...
}