Laravel Language prefix with additional parameters - laravel

Quick question that is already killing me for days.
With Laravel I am trying to use different languages.
English and Japanese
This works in the route like this.
Route::group([
'prefix' => '{lang}',
'where' => ['lang' => '(jp|en)'],
'middleware' => 'Language'
], function() {
Route::get('/blogs', 'BlogController#index')->name('main-blog');
Route::get('/blog/{postId}/{postTitle}', 'BlogController#post');
});
This works when I am visiting the "/blogs" page.
It changes between languages.
Now when I visit the "/blog/{postId}/{postTitle}" page I can't access the posted parameter anymore within my controller.
Somehow it only shows the "lang" parameter.
What would be the right way to access a parameter when using a prefix.
When I don't use the prefix it works like a charm.
My Controller;
public function post($blog_id, $blog_title)
{
// Do something
}
Help is highly appreciated.
I have been banging my head with this for days now.
Wesley

You use the prefix parameter to specify common parameters for your grouped routes. So You need one more parameter $lang for this controller:
public function post($lang, $blog_id, $blog_title)
{
// Do something
}
With prefix parameter the routes look like these:
/{lang}/blogs
/{lang}/blog/{postId}/{postTitle}

Related

Using route actions with new laravel route syntax

I have question about syntax which I can not find an answer for.
I have code in routes file:
Route::get(
'/something/{seoString}/{someMore}',
['as' => 'my_name', 'uses' => '\my\namespace\MyController#index', 'my_route_action' => 20]
);
And I would like to rewrite it using the new syntax for calling controller
Route::get(
'/something/{seoString}/{someMore}',
[MyController#::class, 'index'] // would like to use this new syntax
);
And it works fine, but how can I add the custom route action 'my_route_action'?
I know it's possible to wrap the routes with a group and add it this way:
Route::group(['my_route_action' => 20], static function () {
Route::get(
'/something/{seoString}/{someMore}',
[MyController#::class, 'index'] // would like to use this new syntax
);
);
But that's not what I'm looking for. I don't want to be adding one group for each route just to add the route action.
So I wanted to ask if it does exist something like ->addCustomAction() or how is this supposed to be done?
Unfortunately the route action is not a thing, and probably shouldn't be. Unsure what you're actually trying to achieve with that too.
If you're passing in GET data like a bit of data, you can do it through: {variable} so the URL would become the following:
Route::get('my-route-url/{model}/get', [MyController::class, 'methodName')->name('something')->middleware(['something'])
And in your controller, you dependency inject request if you're wanting to use that too, as well as the model:
public function methodName(Request $request, Model $model)
{
dd($request->all(), $model);
}
The "as" is the the name method. Middleware is still middleware.
If you're trying to do a Key/Pair bit of data, you need to use POST request and pass it in the data, which you can access via the $request->input('keyName') method in the controller.

Laravel Route Controller issue

I am trying to add a new route to my application and can't seem to get it to work. I keep getting a 404 error. It looks like the physical path is looking at the wrong directory. Currently looking at D:\Web\FormMapper\blog\public\forms but should be looking at D:\Web\FormMapper\blog\resources\view\layout\pages\forms.blade.php
My request URL:
http://localhost/FormMapper/ /works fine
http://localhost/FormMapper/forms /doesn't work
http://localhost/FormMapper/forms.php /No input file specified.
my FormsController:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormsController extends Controller
{
public function index()
{
return view('layouts.pages.forms');
}
}
My web.php:
Route::get('/', function () {
return view('layouts/pages/login');
});
Route::get('/forms', 'FormsController#index');
My folder structure looks like this:
My config/view.php
return [
'paths' => [
resource_path('views'),
],
'compiled' => env(
'VIEW_COMPILED_PATH',
realpath(storage_path('framework/views'))
),
];
you must use dot for this. In your controller change to this:
return view('layouts.pages.forms');
If your route only needs to return a view, you may use the Route::view method. Like the redirect method, this method provides a simple shortcut so that you do not have to define a full route or controller. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument:
Route::view('/', 'layouts.pages.login');
Route::view('/forms', 'layouts.pages.forms', ['foo' => 'bar']);
Check docs
After tracking digging deeper I determined that the issue was that IIS requires URL rewrite rules in place for Laravel to work properly. The index.php and '/' route would work b/c it was the default page but any other pages wouldn't. To test this I used the
php artisan serve
approach to it. and everything worked properly. Unfortunately I am unable to do this in production so I needed to get it to work with IIS.

Multi-language site. Set routing's rules

I was created this simple routing
Route::group(['prefix' => '{lang}'], function(){
Route::get('/hello', function($lang){
App::setlocale($lang);
return view('welcome');
});
});
It works of course, but in this example I returned only view. I prefer returned controller's method so in my previous project I realize routing in this way:
Route:get('/hello', [
'uses' => 'MyController#myMethod',
'as' => 'myMethod'
]);
How can I use localization and returned controller's method.
You can implement it through the Accept-Language header or the parameter that will determine which language, for example /route/{language}. Next, send a request to the rout, which will call the method in the controller and already in the method to check - which language to use.
Another option is to create multiple routines for multiple languages. And ask for rows depending on the language. There is a minus, if many languages ​​are used, you need to create a lot of routes

how to set common controller in group route

I am working on laravel but i have no idea about using route.
i used route group method but i have a question that can we use a common controller in group route
like
I have bunch of routes
Route::group(['prefix' => 'agent'], function(){
Route::get('pay', 'PaymentController#pay');
Route::get('pay/success', 'PaymentController#success');
Route::get('pay/failure', 'PaymentController#failure');
Route::get('credits', 'PaymentController#credits');
Route::get('checkout', 'PaymentController#checkout');
});
As you can see they all are using same route so is there any way to make this as dry as possible i know those are small route but when it goes long line then it become hard to understand i know it's kind of stupid question
is there any attribute like
Route::group(['prefix' => 'agent', 'controller' => 'PaymentController'], function(){
Route::get('pay', 'pay');
Route::get('pay/success', 'success');
Route::get('pay/failure', 'failure');
Route::get('credits', 'credits');
Route::get('checkout', 'checkout');
});
No there isn't any option to define default controller for a route group. But if you have resource routes then it defines all the sub routes by itself, though it's limited to only CRUD routes. You can do something like this if you're interested.
Route::group(['prefix' => 'agent'], function($controller = 'TestController#') {
Route::get('pay', $controller.'pay');
Route::get('pay/success', $controller.'success');
Route::get('pay/failure', $controller.'failure');
Route::get('credits', $controller.'credits');
Route::get('checkout', $controller.'checkout');
});

How do I set dynamic prefixes in Laravel routes

heres the situation - I'm building a site thats region based and I need to set it up so that the first segment of the route is a region i.e. of the type:
www.mysite.com/{region}
Currently I have routes set up like this:
www.mysite.com/businesses
www.mysite.com/businesses/show
I've tried a number of tricks but for some reason I can't get this to work:
www.mysite.com/{region}/businesses
in such a way that I need to filter by the {region} variable and that the {region} variable has to prepend every url in the site, also the {region} variable should also be accessible in the controller. I had a look at the localization options except I don't want to change languages here. I'm looking for implementing something of the following:
www.mysite.com/barcelona/businesses
www.mysite.com/new-york/businesses
I already have a table of all regions and slugs and the required relationships. Just trying to get this to work.
Add the region route on top of all other routes, I have similar feature for a project and that fixed it for me.
Route::get('{region?}/businesses', array(
'as' => 'regionBusinesses',
'uses' => 'RegionBusinessesController#getBusinesses'
))->where('region', '.*');
In your Controller
class RegionBusinessesController extends SomeController {
public function getBusinesses($region) {
return View::make('YOUR_VIEW_NAME')->withBusinesses($this->YOUR_MODEL->FETCH_BUSINESSES_METHOD($region));
}

Resources