I'm trying to have access to {module} inside my function, but it returns me the following error :
Too few arguments to function {closure}(), 1 passed in /Users/Bernard/PROJECTS/myproject/vendor/laravel/lumen-framework/src/Routing/Router.php on line 62 and exactly 2 expected
Here's my code :
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
$router->group([
'namespace' => $version,
'prefix' => "api/$version/{contest}/{module}",
'middleware' => 'App\Http\Middleware\COMMON\DefineContest',
], function ($request, $module) use ($router) {
dd($module);
require __DIR__ . "/../routes/v1/{module}.routes.php";
});
});
In Laravel, it would be as simple as calling Illuminate\Support\Facades\Route::parameter(paramname) but since it is apparently not available in Lumen(looking at Lumen Router there are no methods or parameters for this use case), this is an imperative answer that does get the job done:
$version = 1;
$router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) use ($version) {
$router->group([
'namespace' => $version,
'prefix' => "api/$version/{contest}/{module}",
'middleware' => 'App\Http\Middleware\COMMON\DefineContest'
], function ($router) use ($version) {
$url = $_SERVER['REQUEST_URI'];
if (preg_match("/api\/$version\/(?<contest>\w+)\/(?<module>\w+)/", $url, $output)) {
$module = $output['module'];
dd($module);
}
});
});
I should point out that this code results in an extra step for every route which I don't think matters since it's not that heavy but something to note of.
Related
I've read that it's possible to specify namespace for Dingo in that way
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers'], function ($api) {
$api->get('sites', 'SiteController#index');
$api->get('sites/{site}', 'SiteController#show');
$api->post('sites', 'SiteController#store');
$api->put('sites/{site}', 'SiteController#update');
$api->delete('sites/{site}', 'SiteController#delete');
});
However, I've got multiple files with API routes so that I don't want to mess it with namespace and would like to specify namespace in Laravel way like that
protected function mapDingoApiRoutes() {
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
], function ($api) {
require base_path('routes/api/sites.api.php');
require base_path('routes/api/skills.api.php');
require base_path('routes/api/socials.api.php');
});
}
However it turned out that Dingo doesn't see this namespace declaration and I'm not able to make something like $api->group()
Solved that way
...
use Dingo\Api\Routing\Router;
class RouteServiceProvider extends ServiceProvider
{
...
public function map()
{
$this->mapDingoApiRoutes(app('Dingo\Api\Routing\Router'));
$this->mapWebRoutes();
}
protected function mapDingoApiRoutes(Router $api) {
$api->group([
'version' => 1,
'middleware' => 'api',
'namespace' => $this->namespace,
], function ($api) {
require base_path('routes/api/sites.api.php');
require base_path('routes/api/skills.api.php');
require base_path('routes/api/socials.api.php');
});
}
...
}
I want to build multilingual website with laravel and I want to make urls seo friendly.
So I added language prefix to whole routes using RouteServiceProvider
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
'prefix' => $this->app->getLocale()
], function ($router) {...);
Are we should any parameter for the language ? Because I didin't use any parameter for routes..
Route::group(["prefix"=>trans("routes.admin")],function (){
Route::get("/",["as"=>"admin.index","uses"=>"AdminController#index"]);
});
I research about it on stackoverflow, laravel.com but I couldn't find anything useful.
How do I can fix this error ? Any help would be appreciated.
$this->app->getLocale() return string, try this:
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
'prefix' => '{locale?}'
], function ($router) {
$router->get("admin",["as"=>"admin.index","uses"=>"AdminController#index"]);
});
I have made a package in Laravel 5.4 where i do routing. It looks like this in my
serviceprovider:
public function boot()
{
$routeConfig = [
'namespace' => 'LarsJanssen\GetJson\Controllers',
'prefix' => 'api',
'middleware' => [
'api',
],
];
$this->getRouter()->group($routeConfig, function ($router) {
$router->get('/feeddex/{store}', [
'uses' => 'GetJsonController#store',
'as' => 'getjsonproperly.store',
]);
});
}
The problem is that the parameter store never works!
The controller method looks like this:
public function store($store)
{
//my code
}
Error message:
Missing argument 1
What could I be doing wrong here?
"You may also change the active language at runtime using the setLocale method on the App facade:"
https://laravel.com/docs/5.3/localization#introduction
Route::get('welcome/{locale}', function ($locale) {
App::setLocale($locale);
//
});
How do we do that with $locale if we have something like this:
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function( $locale ) {
// this does not work.
App::setLocale( $locale );
// this does work.
App::setLocale( Request::segment( 3 ) );
Route::resource('product', 'ProductController', ['except' => [
'show'
]]);
});
The issue is with route parameters not with localization
Since you expect two params for the route you should pass two params for the closure.
Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
//
});
In your example
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function( $id, $locale ) {
// this does not work.
App::setLocale( $locale );
// this does work.
App::setLocale( Request::segment( 3 ) );
Route::resource('product', 'ProductController', ['except' => [ 'show' ]]);
});
Refer route parameters for more info
Please note - Route::group callback executes on any request (even not for your prefix!). Route::group is not similar to Route::get/post/match, it is like a helper for internal get/post/match calls.
App::setLocale( $locale ); does not work because Laravel passes only instance of Illuminate\Routing\Router into group's callback. On this stage locale prefix has not been extracted and even URL has not been processed.
App::setLocale( Request::segment( 3 ) ); will be executed for 'one/two/three' with 'three' as a locale.
Your example should be:
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function() {
// test a locale
Route::get('test', function($locale){ echo $locale; });
// ProductController::index($locale)
Route::resource('product', 'ProductController', ['except' => [
'show'
]]);
});
So, just update your ProductController and add $locale as a parameter.
Alternative: if you want setLocale in one place update your routes:
// set locale for '/admin/anything/[en|fr|ru|jp]/anything' only
if (Request::segment(1) === 'admin' && in_array(Request::segment(3), ['en', 'fr', 'ru', 'jp'])) {
App::setLocale(Request::segment(3));
} else {
// set default / fallback locale
App::setLocale('en');
}
Route::group(['prefix' => 'admin/{id}/{locale}', 'namespace' => 'Admin'], function() {
Route::resource('product', 'ProductController', ['except' => [
'show'
]]);
});
We're currently working on a Laravel 5 project that hosts separate sub-websites. These separate websites are grouped in the routes and share common prefixes. For example:
Route::group(['prefix' => 'siteone', 'namespace' => 'SiteOneNamespace'], function() {
Route::get('routeone', 'SiteOneController#index');
Route::get('routetwo', 'SiteOneController#indextwo');
(...)
}
Route::group(['prefix' => 'sitetwo', 'namespace' => 'SiteTwoNamespace'], function() {
Route::get('routeone', 'SiteTwoController#index');
Route::get('routetwo', 'SiteTwoController#indextwo');
(...)
}
A third party library is used by all sites in this project. This third party library relies on PHP constants for its settings. However, not all sub-sites will have the same settings, as some of the settings will differ depending on each site.
So my question is: Is there a way that I can define these constants based on the 'prefix' value of each sub-site route, in a way that these constants will be available in the controllers?
Something like:
$routePrefix = getRoutePrefix();
if($routePrefix == 'siteone') {
define("LIBRARY_SETTING", "value_for_site_one");
}
elseif($routePrefix == 'sitetwo') {
define("LIBRARY_SETTING", "value_for_site_two");
}
I know that we can probably do this in the routes.php file, but I think there must be a more elegant solution for this, as the routes file isn't supposed to be a place to define constants. I appreciate any input.
You can do it in a middleware:
namespace App\Http\Middleware;
use Closure;
class CreateConstant
{
public function handle($request, Closure $next, $name, $value)
{
define($name, $value);
return $next($request);
}
}
Then register it in the App\Http\Kernel class:
protected $routeMiddleware = [
// other route middleware...
'constant' => 'App\Http\Middleware\CreateConstant',
];
Finally, use it on your route groups:
Route::group([
'prefix' => 'siteone',
'namespace' => 'SiteOneNamespace',
'middleware' => 'constant:LIBRARY_SETTING,value_1',
], function() {
// routes
});
Route::group([
'prefix' => 'sitetwo',
'namespace' => 'SiteTwoNamespace',
'middleware' => 'constant:LIBRARY_SETTING,value_2',
], function() {
// routes
});