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"]);
});
Related
iam new in laravel , and i wrote this code at routes/api.php in laravel 9
Route::group([
'prefix' => 'auth',
'namespace' => 'Auth'
], function(){
Route::post('register', 'RegisterController');
});
and then i got cant run php artisan serve , it said
UnexpectedValueException
Invalid route action: [Auth\RegisterController].
at G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:92
88▕ */
89▕ protected static function makeInvokable($action)
90▕ {
91▕ if (! method_exists($action, '__invoke')) {
➜ 92▕ throw new UnexpectedValueException("Invalid route action: [{$action}].");
93▕ }
94▕
95▕ return $action.'#__invoke';
96▕ }
1 G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\RouteAction.php:47
Illuminate\Routing\RouteAction::makeInvokable("Auth\RegisterController")
2 G:\PRODUCTIVITY\SANBERCODE\LARAVEL-VUE\TUGAS\laravel-vue-crowdfunding-website-batch-37\crowdfunding-website\vendor\laravel\framework\src\Illuminate\Routing\Route.php:190
Illuminate\Routing\RouteAction::parse("api/auth/register", ["Auth\RegisterController", "Auth\RegisterController"])
someone please help me :)
Add RegisterController function
Route::group([
'prefix' => 'auth',
'namespace' => 'Auth'
], function(){
Route::post('register', 'RegisterController#store');
});
You are missing a parameter in your post function from Route.
You want something like
Route::post('route_name', 'Controller#myFunction')
Or in your case:
Route::post('register', 'RegisterController#registerFunctionName');
Other variation per 9.x documentation:
Route::post('register', [RegisterController::class, 'registerFunctionName']);
Please refer to:
https://laravel.com/docs/9.x/routing
This is an invokable controller yes?
you need to just alter the syntax
Route::group([
'prefix' => 'auth',
'namespace' => 'Auth'
], function(){
Route::post('register', [RegisterController::class]);
});
and then import the class at the top of your routes file and make sure you have a single public method of __invoke() in your controller.
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.
i just want to group all my admin routes in my laravel. I'm a beginner in laravel and i want to synchronize all my admin routes in one group, my question is, why i cant put the post route inside the group of my admin routes?
Here is my routes:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
});
my above code was returning error , where laravel says admin.login route doesn't exist. Then i tried to put the post route outside the group and it works. Why?.
Here is the code where returns no error:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
});
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
Because you use as in your route group and it's admin:: and you may link to admin.
Now it goes to admin::login and you need admin.login
I'm trying to implement a simple CMS with Laravel 5.2 which basically handles two kinds of routes. The first one is used to browse a website view, that has to be {view}.html. The controller iterates the database records and if it can't find that page, will return a 404 error page:
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
For example these routes will match:
www.mydomain.ext/homepage.html
www.mydomain.ext/about.html
www.mydomain.ext/news.html
www.mydomain.ext/contact.html
and so on. The second one is a route group for the admin control panel:
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
So all the routes in this group will be something like:
www.mydomain.ext/admin/dashboard
www.mydomain.ext/admin/user/1
www.mydomain.ext/admin/page/2
and so on.
From what I've found here:
Laravel matches routes from the top down. So all you need to do is put 'campaigns/add' above the wildcard route.
And that's what I've done:
routes.php
Route::group([
'prefix' => Localization::setLocale(),
'middleware' => ['localeSessionRedirect', 'localizationRedirect']
// LaravelLocalization (https://github.com/mcamara/laravel-localization)
], function() {
Route::auth();
// admin routes
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
});
But when I try to call an admin route, Laravel throws this error:
Missing argument 1 for App\Http\Controllers\Website\Core\WebsiteCoreController::showPage()
So I suppose I'm doing something wrong... Any suggestions on how to fix my code?
Thanks everyone in advance
I have the following routes in place:
Route::group(['prefix' => 'api/v1', 'middleware' => 'api'], function() {
Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
Route::post('authenticate', 'AuthenticateController#authenticate');
Route::resource('users', 'UserController');
});
The UserController has a test to ensure that when a user is submitted via POST, that it validates the input correctly. This should return a 422 when invalid, but it actually returns a 302. In Postman, it raises a CSRF token error, suggesting the web middleware group is being applied, which is not the behaviour I want.
How can I prevent this happening?
In RouteServiceProvider.php change
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web',
], function ($router) {
require app_path('Http/routes.php');
});
to:
$router->group([
'namespace' => $this->namespace,
], function ($router) {
require app_path('Http/routes.php');
});
And then wrap your web routes with Route::group(['middleware' => 'web']) in routes.php. So api routes will be not affected by web middleware.