I've run into a problem with the updated version of Laravel and the new routing.
The route with the resources works just fine and uses the correct namespace, the problem is with the direct route "users/table", it's not using any namespace and returns "Target class [UserController] does not exist."
.
When I apply the full controller namespace and class name it works. I've modified my RouteResourceProvider.php and it's loading the default namespace on boot.
My question is why the resources method works but for the custom route, I have to specify the entire namespace inside the route group with an already set namespace?
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'Admin'], function () {
Route::post('users/table', [UserController::class, 'table']);
...
Route::resources([
'users' => UserController::class,
...
]);
});
Route::group(['prefix' => 'admin', 'as' => 'admin.', 'namespace' => 'App\Http\Controllers\Admin'], function () {
Route::post('users/table', [UserController::class, 'table']);
...
Route::resources([
'users' => UserController::class,
...
]);});
Related
I have created ApiController in App\Http\Controllers\Api\v1
Also created auth using laravel/ui
Default created function for front end working perfectly.
But issue is when try to call the ApiController
My API Route file is as below
Route::group(['prefix' => 'api/v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController#register');
});
And my API controller look like
namespace App\Http\Controllers\Api\v1;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ApiController extends Controller
{
public function register(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
}
Before 404 it was csrf error and i have resolving it by
protected $except = [
'/register',
];
in Http\Middleware\VerifyCsrfToken
I cant figure out two question
How to except my entire api call from CSRF using $except..
How to solve 404 for register method , i use postman with POST request and call URL http://localhost/larablog/api/v1/register
Routes defined in the routes/api.php file are nested within a route group by the RouteServiceProvider. Within this group, the /api URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider class.
1) 404 error :- Remove api from prefix route.
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController#register');
});
http://localhost/larablog/api/v1/register
1. If you are using a route group:
Route::group(['prefix' => 'v1', 'namespace' => 'Api\v1'], function () {
Route::post('register', 'ApiController#register');
});
Your $except array looks like:
protected $except = ['v1/register'];
2. If you want to exclude all routes under v1
Your $except array looks like:
protected $except = ['v1/*'];
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
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.
After Installation of laravel-ide-helper I found that I can not navigate to controller methods by ctrl+click on the name of controller only in api.php file. while that works fine in web.php routes file.
Of course I'm using dingo laravel package and all defined routes are like this :
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['prefix' => 'v1', 'namespace' => 'App\Http\Controllers'], function ($api) {
$api->post('Profile', ['as' => 'Profile', 'uses' => 'UserController#showProfile']);
$api->post('getEditProfile', ['uses' => 'loginController#getEditProfile']);
$api->post('UpdateProfile', ['uses' => 'loginController#UpdateProfile']);
});
Could this be problematic?
I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);