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.
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 am beginner in Laravel. I have project in Laravel 6
I have this code in routes/admin.php:
Route::name('admin.')->group(function(){
Auth::routes(['register' => false, 'reset' => false, 'confirm' => false, 'verify' => false]);
Route::get('/', 'HomeController#index')->name('admin.home');
Route::get('/', 'HomeController#index')->name('home');
});
Route::group(['prefix' => ''], function () {
//Auth::routes(['register' => false, 'reset' => false, 'confirm' => false, 'verify' => false]);
/* Pages */
Route::get('/pages', 'PageController#index')->name('page.index');
Route::get('/pages/create', 'Admin\PageController#create')->name('page.create');
Route::post('/pages/store', 'Admin\PageController#store')->name('page.store');
Route::get('/pages/edit' . '/{id?}', 'Admin\PageController#edit')->name('page.edit');
Route::put('/pages/update', 'Admin\PageController#update')->name('page.update');
Route::delete('/pages/destroy'. '/{id?}', 'Admin\PageController#destroy')->name('page.destroy');
/* Users */
Route::get('/users', 'Admin\UserController#index')->name('user.index');
Route::get('/users/create', 'Admin\UserController#create')->name('user.create');
Route::post('/users/store', 'Admin\UserController#store')->name('user.store');
Route::get('/users/edit' . '/{id?}', 'Admin\UserController#edit')->name('user.edit');
Route::put('/users/update', 'Admin\UserController#update')->name('user.update');
Route::delete('/users/destroy'. '/{id?}', 'Admin\UserController#destroy')->name('user.destroy');
});
All from this URL need login user (admin).
In RouteServiceProvider I have:
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
protected function mapAdminRoutes()
{
Route::prefix(config('app.admin_prefix'))
->middleware('web')
->namespace($this->namespace.'\Admin')
->group(base_path('routes/admin.php'));
}
protected function mapAdminOnlyRoutes()
{
Route::middleware('web')
->namespace($this->namespace. '\Admin')
->group(base_path('routes/admin.php'));
}
How can I change my admin.php?
Now user which is no login can view my url
CODE AFTER UPDATE:
Code after update. Is this code written in an optimal way?
Route::name('admin.')->group(function(){
Auth::routes(['register' => false, 'reset' => false, 'confirm' => false, 'verify' => false]);
Route::get('/', 'HomeController#index')->name('admin.home');
Route::get('/', 'HomeController#index')->name('home');
});
Route::group(['prefix' => ''], function () {
/* Pages */
Route::get('/pages', 'PageController#index')->middleware('auth')->name('page.index');
Route::get('/pages/create', 'PageController#create')->middleware('auth')->name('page.create');
Route::post('/pages/store', 'PageController#store')->middleware('auth')->name('page.store');
Route::get('/pages/edit' . '/{id?}', 'PageController#edit')->middleware('auth')->name('page.edit');
Route::put('/pages/update', 'PageController#update')->middleware('auth')->name('page.update');
Route::delete('/pages/destroy'. '/{id?}', 'PageController#destroy')->middleware('auth')->name('page.destroy');
/* Users */
Route::get('/users', 'UserController#index')->middleware('auth')->name('user.index');
Route::get('/users/create', 'UserController#create')->middleware('auth')->name('user.create');
Route::post('/users/store', 'UserController#store')->middleware('auth')->name('user.store');
Route::get('/users/edit' . '/{id?}', 'UserController#edit')->middleware('auth')->name('user.edit');
Route::put('/users/update', 'UserController#update')->middleware('auth')->name('user.update');
Route::delete('/users/destroy'. '/{id?}', 'UserController#destroy')->middleware('auth')->name('user.destroy');
});
Let's say you want to protect Route::get('/users', 'Admin\UserController#index')->name('user.index'); route so that only users may access it, you need to do like so:
Route::get('/users', 'Admin\UserController#index')->middleware('auth')->name('user.index');
But if you need to open it to only admin and not other users, you can create your own middleware. See docs https://laravel.com/docs/7.x/middleware#defining-middleware.
Then you need to register your middleware in $routeMiddleware in App\Http\Kernel.php. For example, you register your middleware as admin. Then you would protect your route like so:
Route::get('/users', 'Admin\UserController#index')->middleware('admin')->name('user.index');
in the first step you can use the resource method to create create, store, show, edit, update and delete routes. like this:
Route::resource('shops', 'ShopController');
and for authenticated users for can use the auth middleware to your routes like these ways:
Route::get('profile', ['middleware' => 'auth', function()
{
// Only authenticated users may enter...
}]);
// With A Controller...
Route::get('profile', ['middleware' => 'auth', 'uses' => 'ProfileController#show']);
Route::group(['prefix' => 'backend', 'as' => 'backend.', 'namespace' => 'Backend', 'middleware' => ['web', 'auth], function(){})
I have guard 'admin'.
how I know in Laraver guard work as multi auth(I can log in as a user and as Admin at once).
but I pretty surprise when I have an error about ERR_TOO_MANY_REDIRECTS when I log in as admin and try to check the page with auth middleware. Like admin this si no auth user no auth?
Have code like this
Route::group([ 'namespace' => 'Shop','middleware' => ['auth'], 'prefix' => 'shop'], function () {
Route::get('/', 'MainController#index')->name('shop');
Route::get('/search', 'MainController#search')->name('shop.search');
Route::get('/ajax-search', 'MainController#ajaxSearch')->name('shop.ajax-search');
Route::get('autocomplete', 'MainController#autocomplete')->name('shop.autocomplete');
Route::get('/searchbyname', 'MainController#searchbyname')->name('shop.searchbyname');
});
Route::group(['prefix' => 'cart','middleware' => ['auth:admin'], 'namespace' => 'Shop'], function () {
Route::get('/','CartController#index')->name('shop.cart');
Route::get('/add','CartController#add')->name('cart.add');
Route::get('/details','CartController#details')->name('cart.details');
Route::delete('/{id}','CartController#delete')->name('cart.delete');
});
how I can resolve this issue if I wanna that admin has all access as Authentication user + admin routes?
You can declare 2 middlewares in the Route::group. E.g.:
Route::group([ 'namespace' => 'Shop','middleware' => ['auth', 'auth:admin'], 'prefix' => 'shop'], function () {
...
});
maybe this helps you
Route::get('/', function () {
...})->middleware('first', 'second');
multiple middleware to the route
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"]);
});
In Laravel 5.4 I want to prefix locale with base url.When i run php artisan serve then i get
notfoundhttpexception
. It does work if i put manually http://localhost:8000/en. what i want now, when i will run php artisan serve it should redirect to http://localhost:8000/en.
Following is the route file:
Route::group( ['prefix' => App::getLocale() ], function()
{
Route::get('/', array('as' => 'home.index', 'uses' => 'HomeController#getIndex'));
});
If I understand your problem properly, You need use code like this:
Route::group( ['prefix' => '{locale}' ], function()
{
Route::get('/', array('as' => 'home.index', 'uses' => 'HomeController#getIndex'));
//Setup locale based on request...
App::setLocale(Request::segment(1));
});
But the better way I can suggest is use locale setup like:
if (in_array(Request::segment(1), ['en', 'fr'])) {
App::setLocale(Request::segment(1));
} else {
// set your default local if request having other then en OR fr
App::setLocale('en');
}
And just call route like:
Route::group( ['prefix' => '{locale}' ], function()
{
Route::get('/', array('as' => 'home.index', 'uses' => 'HomeController#getIndex'));
});
By this code you can setup locale based on route prefix dynamically.