Laravel 8 laravel change language in url - php

I've been searching a lot and trying a lot of tutorials, and I haven't been able to get it to work.
The case, is that I want to set the language in each request that I send through the url, since I operate through an API, so, in each call, the language parameters would be sent like this "localhost/public/api/en/createuser", I have followed this tutorial, which does what I need, but at least in laravel 8 has not worked.
https://irando.co.id/articles/how-to-use-language-slug-in-laravel-url-and-set-directions

Firstly you should add prefix to top of the api routes. For example;
use App\Libraries\DetectLanguage;
Route::prefix(DetectLanguage::setLanguageCodeByPrefix())->group(function () {
/* your routes in api.php */
}
DetectLanguage must have setLanguageCodeByPrefix() method.
<?php
namespace App\Libraries\DetectLanguage;
class DetectLanguage
{
public static setLanguageCodeByPrefix()
{
$languagePrefix = request()->segment(1); // For example en
// You can use this define any area in code
define('REQUEST_LANGUAGE', $languagePrefix);
return $languagePrefix;
}
}

I followed solution provided by your link
https://irando.co.id/articles/how-to-use-language-slug-in-laravel-url-and-set-directions
and edit the following code in RouteServiceProvider.php and it works
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
$locale = \Request::segment(1);
app()->setLocale($locale);
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->prefix($locale)
->group(base_path('routes/web.php'));
});
}

Related

file RouteServiceProvider

I'm having a problem adding root link files to a laravel project
This is the file Route:
use Illuminate\Support\Facades\Route;
Route::prefix("dashboard")->group(function(){
Route::get('/test', function () {
return '<h1>test</h1>';
})->name('test');
});
These are the functions in the RouteServiceProvider file:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
Route::middleware('web')
->group(base_path('routes/web.php'));
Route::middleware('web')
->group(base_path('routes/dashboard.php'));
});
}
But I still get the error (404 page not found):
404
NOT FOUND
Please help generate the code, thanks
You guys have really great information

Target class [Admin\UserController] does not exist

I have separated my project routes into home.php that contains the client-side routes and admin.php that contains server-side routes.
So here is my RouteServiceProvider.php:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web/home.php'));
Route::middleware(['web', 'auth.admin'])
->namespace($this->namespace . '\Admin')
->prefix('admin')
->group(base_path('routes/web/admin.php'));
});
}
So as you see I have specified ->namespace($this->namespace . '\Admin') because of Admin Controllers that are placed in this directory:
App\Http\Controllers\Admin\...
Then in the admin.php, I added this route:
Route::resource('users', UserController::class);
But I get this error:
Target class [Admin\UserController] does not exist.
So what's going wrong here? How can I solve this issue and properly call the Controller from Admin?
You might need to add a use statement to point out where your controller lies within the route file.
For example:
use App\Http\Controllers\Admin\UserController;
The namespace changes are correct.
The problem is a missing using.
So to correct it, change in your route file:
use App\Http\Controllers\Admin\UserController; // <-- add this.
Route::resource('users', UserController::class);
You need to adjust this for the RouteServiceProvider.php:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace('\App\Http\Controllers')
->group(base_path('routes/web/home.php'));
Route::middleware(['web', 'auth.admin'])
->namespace('\App\Http\Controllers\Admin')
->prefix('admin')
->group(base_path('routes/web/admin.php'));
});
}
And then you can run this:
Route::resource('users', 'UserController');

Laravel - subdomain routing not working as expected

I have three routing files inside routes folder:
web.php
api.php
admin.php
I've registered the file admin.php inside boot() method of RouteServiceProvider as following:
public function boot() {
$this->configureRateLimiting();
$this->routes(function () {
Route::domain("admin." . env("APP_URL"))
->namespace($this->namespace)
->group(base_path("routes/admin.php"));
Route::domain("api." . env("APP_URL"))
->middleware("api")
->namespace($this->namespace)
->group(base_path("routes/api.php"));
Route::middleware("web")
->namespace($this->namespace)
->group(base_path("routes/web.php"));
});
}
Let's say i've defined the following route in web.php:
Route::get("test", function() {
return "Hello from website route";
});
If i try to access this route using mywebsite.com/test it acts like expected.
However, if i try to access the link admin.mywebsite.com/test it would still some how falls back to mywebsite.com/test and give the exact same output.
What's the problem here ?
Because web.php is not restricted to a domain. As a consequence, the routes defined there are accessed by every domain that targets your laravel application.
If you only want the root domain to access the routes defined in your web.php, you could specify it in the RouteServiceProvider:
Route::middleware("web")
->domain(env("APP_URL")) // Add this line
->namespace($this->namespace)
->group(base_path("routes/web.php"));

What is the best practice to add domain based routing in Laravel?

I'm working on a multitenant, multidomain Laravel project and of course the base of the software is the same for all users, but I have several users who asking me very specific things and don't want to build these features into the main codebase.
I separated the models, controllers, and migrations to namespaces like this:
app/Models/ClientSpecific/{ClientName}/{ModelName}
app/Http/Controllers/ClientSpecific/{ClientName}/{ModelName}Controller
database/migrations/client-specific/{ClientName}/{MigrationFile}
Going along this logic I want to add routes and I want to store it in separate files too:
routes/client-specific/{domainname.com}/api.php
routes/client-specific/{domainname.com}/web.php
routes/client-specific/{anotherdomain.com}/api.php
routes/client-specific/{anotherdomain.com}/web.php
routes/client-specific/{thirddomain.com}/api.php
How can load these route files only that domain what is called and only there? Is there any best practice to add domain based routing in Laravel? Or just add a domain check to the RouteServiceProvider.php based on $_SERVER['SERVER_NAME'] ?
I suggest you store your domains in the .env file. That's will allow you to configure them in different environments.
APP_CLIENT_DOMAIN=client.com
APP_ADMIN_DOMAIN=admin.com
APP_DEVELOPER_DOMAIN=developer.com
Then, you can define different RouteServiceProviders for each domain, where each service provider will provide api.php and web.php routes for each domain.
Example:
This will be your BaseRouteServiceProvider:
abstract public function getDomain();
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace(
$this->namespace."\".Str::toCamelCase($this->getDomain()))
->group(base_path('routes/'.$this->getDomain().'/web.php'));
}
protected function mapApiRoutes()
{
Route::middleware('api')
->namespace(
$this->namespace."\".Str::toCamelCase($this->getDomain()))
->group(base_path('routes/'.$this->getDomain().'/api.php'));
}
Then you can define FirstDomainServiceProvider, SecondDomainServiceProvider, and more. In most cases, they will look like:
public function getDomain() {
returt env('APP_CLIENT_DOMAIN')
}
I created this solution, but maybe there is a better way. If you found it, please post it.
Thanks!
I changed the code in the RouteServiceProvider.php to this:
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
// load client sepcific routes
$host = request()->getHost();
if (isset($host)) {
try {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/client-specific/'.$host.'/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/client-specific/'.$host.'/web.php'));
} catch (\Throwable $th) {
// do nothing
}
}
});
}

How to add multiple route file using RouteServiceProvider in laravel

I want to create module wise route file and load all route files using RouteServiceProvider mapApiRoutes(). I have created category.php file and admin.php file which contains routes within it. Now i want to load this two file's routes in api.php file.
Below is code that i am using to do this but it is not working. it only process routes in admin.php. when i use route of category.php it shows me error of "Sorry, the page you are looking for could not be found.". Thank for help in advance.
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(
base_path('routes/admin.php'),
base_path('routes/category.php'),
base_path('routes/api.php')
);
}
I have solved this issue by following code. Hope this will help someone.
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(function ($router) {
require base_path('routes/admin.php');
require base_path('routes/category.php');
});
}

Categories