Laravel locale route get - php

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();
});

Related

Laravel lang slug in url

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

Object of class Illuminate\Routing\Route could not be converted to string

I'm trying to do something very simple.
Route::get('/', function () {
return Route::view('/welcome', 'welcome');
});
I just want to it to load the welcome view and change the URI to /welcome. However, as you can see it keeps throwing the error Object of class Illuminate\Routing\Route could not be converted to string.
I haven't touched Laravel in a minute and am kind of doing a refresher and tried to set up a simple site. I may be missing something totally obvious but I have no idea what it could be.
Any help would be much appreciated.
I think your mean like
Route::redirect('/', '/welcome', 301);
Route::view('/welcome', 'welcome');
or
//one view like resources/views/welcome.blade.php
Route::get('/', function () {
return view('welcome');
});
But in fact we usually use .htaccess redirect request, because you must load all requires before do anything in framework.
You can either use
Route::view('/','welcome');
Or use
Route::get('/', function () {
return view('welcome');
});
I guess you are mixing two different syntax.
[http://www.expertphp.in/article/laravel-5-5-new-feature-route-view-and-route-redirect-method-with-example]

How does Routing work in PHP laravel?

I have just started playing with Laravel framework and I have seen this :
Route::get('foo', function () {
return 'Hello World';
});
Can some one please explain what is this ? I mean over all I know what is get . but why do we put 'foo' and then the closure we put ?
Also where am I really getting the information from ?
First we declare the Facade of the Route, think like a shortcut to use the Route class.
After that, we choose the method of the route, it could be:
Route::get($uri, $callback); //get
Route::post($uri, $callback); //post
Route::put($uri, $callback); //put
Route::patch($uri, $callback); //patch
Route::delete($uri, $callback); //delete
Now you choose the url of the page, for example:
If you digit in the browser:
www.foobar.com/user/profile
Laravel will search for the route with the user/profile parameter, like that:
Route::get('user/profile', function () {
return 'Hello World';
});
You can pass variables too,
Route::get('user/{id}', function () {
return 'Hello World';
});
After that, you choose the callback method, in other words, what is gonna happen when the laravel enter in the route.
In your example, you have the function example, just returning a simple "hello world".
The best pratice here is to create a controller
php artisan make:controller FoobarController --resource
And referece to any method of your controller
Route::get('user/profile', 'FoobarController#index');
Now, when the laravel find the route, it's going to redirect to the index method of the Foobar controller, and there, you define your logic
public function index() {
return view('welcome');
}
Firsty, read the documentation, it's super easy, even for the begginers.
Step by step:
get is the HTTP method you use on this particular route. The other most often used is POST, but there are more of them.
foo is the route, in that case will be: www.example.com\foo. You can put any name as you want and need.
As a second parameter to a Route facade you put closure/name of the controller/view you want to handle endpoint, e.g.
Route::get('foo', 'SomeController#method');
Route::get('foo', function(){
return view('some.view');
};
There are lot more options in routing and they are not difficult to understand, just have a look on documentation or some video tutorials.

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.

How to pass route parameters to webcontroller in Laravel?

I've read from the Laravel manual that I can do this:
Route::get('user/{id}', function ($id) {
return 'User '.$id;
});
I've also read that you can pass the route directly to webcontroller:
Route::get('user', 'WebController#profile');
But what I can't find is how to pass the variable id to the webcontroller.
Route::get('user/{id}', 'WebController#profile');
// how so that the function profile at WebController receive the id?
I feel that this is a very basic thing to do, but I can't find it. I'm new to Laravel, so I don't know the keyword for search this. Please help. Thanks.
You can simply write up within your controller
public function profile($id){
//Here $id is having the value that you were passing
}

Categories