Laravel lang slug in url - php

I've followed this article to add multi language into my app and it's working fine, the only issue i need to solve is that languages slug won't add to the url.
Example
based on the article above i get my page in English like:
www.myapp.co/my-slug
and in another language the same (however translation will placed)
what i want is like:
www.myapp.co/en/my-slug
&
www.myapp.co/id/my-slug
etc.
Any idea?

Perhaps you can try my answer here, where I used mcamara's Laravel Localization package to achieve this. It was painless for me :)

Change your route using prefix
Route::prefix('{locale}')->group(function ($locale) {
if (array_key_exists($locale, Config::get('languages'))) {
Session::put('applocale', $locale);
}
// else return 404
Route::get('your-slug', function () {
});
});

May be you can use App::getLocale() as route group prefix and use that to choose language .
In routing
Route::middleware(App::getLocale())->group(function () {
Route::get('/', function () {
// Uses first & second Middleware
});
Route::get('user/profile', function () {
// Uses first & second Middleware
});
});
Retrieve the request parameters in middleware and if require
In your middleware you can put something like a conditional and reconstruct the url if required and then redirect it to the newly constructed url.
$locale = $request->segment(1);
if (in_array($locale, config('app.locales'))) {
\App::setLocale($locale);
return $next($request);
} else {
//
}

Solved
I used this tutorial and it gets me what I needed

Related

Laravel locale route get

Normaly i use
Route::get('{locale}/home', [MachineController::class, "home"]);
But i want to use the $locale variable to set the language
Route::get('{locale}/home', function ($locale) {
App::setLocale(getLocale($locale));
[MachineController::class, "home"];
});
but that returns an empty page, whats the most efficient way to do that?
there is a problem with the code you written.
MVC structure (Wikipedia) defined that you must write App::setLocale(getLocale($locale)); in home method from MachineController class (Laravel Routing).
or you can define your route like below:
Route::get('{locale}/home', function ($locale) {
App::setLocale(getLocale($locale));
(new MachineController())->home();
});
you issue with you code try this way :
Route::get('{locale}/home', function ($locale) {
$current=getLocale($locale);
App::setLocale($current);
(new MachineController())->home();
});

Laravel routing strange behavior

I have a single domain/subdomain project. In order to see the event by slug, I made this route:
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
After my page didn't load correctly, I did a dump in the controller:
public function getView($slug)
{
return $slug;
}
To get to the route I am using this URL: https://example.com/events/slug-example.
The problem is that the route is being hit as I see the response when I change it, but I am not getting the slug, instead I am getting Region object back.
If I do this:
public function getView($region, $slug)
{
return $slug;
}
Then I get the slug back. But I have no idea how is this possible, and how could I do it (I came as another dev on the existing project).
I tried commenting out all the middleware and it is still the same. How can I even make something fill the method if I didn't explicitly say it?
EDIT
I noticed there is binding going on in routes file:
Route::bind('region', function ($value) {
...
});
Now if I dd($value) I get the variable back. How is this value filled? From where could it be forwarded?
Looking quickly it should work, but maybe you was verifying other url.
Make sure you put:
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
routes at the end of routes you showed.
EDIT
If you think that's not the case and you don't have your routes cached you should run:
php artisan route:list
to verify your routes.
EDIT2
After explaining by OPs in comment, domain used for accessing site is:
{region}.example.com
So having $region in controller as 1st parameter is correct behaviour because of route model binding and other route parameters will be 2nd, 3rd and so on.
Instead of
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
});
try
Route::prefix('events')->namespace('Content\Controller')->group(function () {
Route::get('/', 'EventController#getIndex')->name('event.index');
Route::post('load-more-ajax/{region?}', 'EventController#postLoadMoreAjax');
Route::any('sorted-ajax/{region?}', 'EventController#anySortedAjax');
Route::get('category/{category_slug}/{subcategory_slug?}', 'EventController#getCategory');
Route::get('{slug}', 'EventController#getView')->name('event.show');
Route::get('{slug}/edit', 'EventController#getEdit')->name('event.edit');
});

Laravel routes not working properly

I have this code in api.php routes file for laravel 5.4
Route::get('/lectures/{id}', function ()
{
dd("lecture route");
});
Route::get('/lectures/send-request', function ()
{
dd("send-request route");
});
the problem is that when I visit localhost:8000/api/lectures/send-request the output is 'lecture route' which is for this url localhost:8000/api/lectures/{id}
but when I change the orders of routes it will work properly
Route::get('/lectures/send-request', function ()
{
dd("send-request route");
});
Route::get('/lectures/{id}', function ()
{
dd("lecture route");
});
now when I visit localhost:8000/api/lectures/send-request the output is 'send-request route'
so what is wrong ? why this is happening ?
The routes are working exactly as intended. The routes take precedence the same way they're defined.
GET /lectures/{id}
GET /lectures/send-request
You've defined two routes where the second route is basically one of options of the first route. When you access /lectures/send-request it satisfies the condition for the route /lectures/{id} with id set to send-request.
You can switch the order of routes based on your preference, or you could change one of the route.
This is because Laravel matches routes top-down. This means that the first route to match is the one being used. /lectures/send-request matches '/lectures/{id}' and sets $id to 'send-request'.

How to properly define the home (/) route in Laravel?

Let's say I am defining the following route in Laravel 5.3:
Route::resource('bands', 'BandController');
The default route example coming when you start a new Laravel project has the following:
Route::get('/', function () {
return view('welcome');
});
How do I make bands route to be the default one when no controller is called instead of welcome? Meaning /?
I did read docs here but didn't found anything. Any?
Place that block inside laravel/app/routes.php instead of a Controller (4.x)
Place that block inside laravel/app/Http/routes.php instead of a Controller (5.1)
Place that block inside laravel/app/routes/web.php instead of a Controller (5.3)
Route::get('/', function()
{
return view('welcome');
});
You can redirect default to anywhere you want, i.e.:
Route::get('/', function()
{
return Redirect::to( '/bands');
// OR: return Redirect::intended('/bands'); // if using authentication
});
As #Patrick mentioned, you can simply redirect to the /bands route. However, I have to warn you that the redirect will actually change the URL in the navigation pane of the web browser. I would have suggested that you just ask the home route to use the index method of your BandController as follows:
Route::get('/', ['uses'=>'BandController#index']);

Proper way to pass a hard-coded value from route to controller (Laravel)?

I have a PagesController with one action: view.
This action accepts a page argument.
What I want to achieve:
Have a routes example.com/about and example.com/foobar.
When one of this routes is triggered, pass a value predefined in routes file to PagesController#view.
In my routes file:
Route::get('about', function () {
return App::make('App\Http\Controllers\PagesController')->view('about');
})->name('aboutPage');
Route::get('foobar', function () {
return App::make('App\Http\Controllers\PagesController')->view('foobar');
})->name('foobarPage');
It works as expected, but I want to know is there a better and more proper way to achieve the same functionality?
Pass your pages as route parameter:
Route::get('{page}', 'PagesController#view');
//controller
public function view($page)
{
//$page is your value passed by route;
return view($page);
}
So you just want an argument to your action. You can use optional parameters if that argument can be empty. You can read more about it here.
Route::get('{argument?}', 'PagesController#view')->name('page');
And in your PagesController:
public function view($argument = 'default') {
// Your logic
}
The accepted answer is what you want based on what you are doing.
If you really wanted a hardcoded value you can use the 'actions' array part of the route if you wanted.
Route::get('something', ['uses' => 'Controller#page', 'page' => 'something']);
public function page(Request $request)
{
$page = $request->route()->getAction()['page'];
...
}
asklagbox - blog - random tips and tricks
If you don't need the names of the routes like in your example
->name('foobarPage');
you can use something like this
Route::get('{page_name}','PagesController#view')->where('page_name', '(about)|(foobar)');
This will accept only the values passed in the regular expression for the page_name parameter. Other routes will throw a 404 error. I should mention that this technique seems to be valid for applications with one level of url nesting only and should NOT be used as a pattern.
From what I can see above if all you are doing is showing the correct view I would go for
Route::get('{page}', function($page)
{
if (view()->exists($page)) {
return view($page);
}
return abort(404);
});
This prevents you even needing a method in your controller.

Categories