I am building a modular application in laravel. I created a User module and here is the routes:
<?php
Route::group(['middleware' => 'web', 'namespace' => 'Modules\User\Http\Controllers'], function()
{
Route::get('/', 'UserController#index');
Route::get('login', 'LoginController#showLoginForm')->name('login');
Route::post('login', 'LoginController#login');
Route::post('logout', 'LoginController#logout')->name('logout');
});
Route::group(['middleware' => 'admin', 'prefix' => 'user', 'namespace' => 'Modules\User\Http\Controllers'], function()
{
Route::get('register', 'RegisterController#showRegistrationForm')->name('register');
Route::post('register', 'RegisterController#register');
});
The route('login') statement returns the url to login page and it works well. Inside the config.php I need to access this function as follows
<?php
return [
'name' => 'User',
'menu' => [
'weight' => 1,
'item' => [
'Login' => [route('login'), 'guest'],
'Register' => [route('register'), 'guest'],
]
]
];
Inside this file the error Route [login] not defined. is reported. Why is this undefined in there?
I also tried adding the following line
namespace Modules\User\Http\Controllers;
But it still not working
thanks
Related
When I change the file-manager.php to 'routePrefix' => 'file' or 'routePrefix' => 'admin.file' the fm-button page still sends the following request.
Request URL: http://127.0.0.1:8000/file-manager/initialize
I changed the routes.php file as follows and only the routes was modified but the requests within the routes were not modified.
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function () {
Route::group([ 'prefix' => 'file-manager', 'namespace' => 'Alexusmai\LaravelFileManager\Controllers', ], function () {...
In my Laravel 8 app I want to make route of ads/ad_locations editor
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
...
Route::group(['prefix' => 'ads'], function ($router) {
...
Route::resource(
'/{ad_id}/ad_locations',
AdLocationController::class
)->name('admin.ads.ad_locations');
But I got error clearing routes :
$ php artisan route:cache
ArgumentCountError
Too few arguments to function Illuminate\Routing\PendingResourceRegistration::name(), 1 passed in /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/routes/web.php on line 112 and exactly 2 expected
at vendor/laravel/framework/src/Illuminate/Routing/PendingResourceRegistration.php:110
106▕ * #param string $method
107▕ * #param string $name
108▕ * #return \Illuminate\Routing\PendingResourceRegistration
109▕ */
➜ 110▕ public function name($method, $name)
111▕ {
112▕ $this->options['names'][$method] = $name;
113▕
114▕ return $this;
1 routes/web.php:112
Illuminate\Routing\PendingResourceRegistration::name()
+3 vendor frames
5 routes/web.php:119
Illuminate\Support\Facades\Facade::__callStatic()
Which syntax is valid and how to ref in in blade file ?
MODIFIED BLOCK:
I modified in routes/web.php :
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
'edit' => 'admin.ads.ad_locations.edit',
]
]);
and run command with success :
php artisan route:cache
But in blade file using it :
<a class="btn btn-primary" href="{{ route('admin.ads.ad_locations.edit',[$ad_id, $nextAdLocation['id']]) }}">
{!! showAppIcon('edit') !!} Edit
</a>
I got error :
(Symfony\\Component\\Routing\\Exception\\RouteNotFoundException(code: 0): Route [admin.ads.ad_locations.edit] not defined. at /mnt/_work_sdb8/wwwroot/lar/AdsBackend8/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:429)
[stacktrace]
Which way is correct ?
Thanks!
Function name() doesn't work with route::resource, because it's a collection of routes, it should be used with individual routes like:
Route::get('user/create', [UserController::class, 'create'])->name('user.create');
You can either supply a "names" array as the third parameter (options) parameter to the resource route, like:
Route::resource('user', UserController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
// etc...
]
]);
or, you can use with as keyword, like the one you have used in your route:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.ads.ad_locations.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class);
});
});
In the above snippet, I have used your complete route "admin.ads.ad_locations" name in "as" attribute's value. But, this last one will be treated as prefix for every single route like this:
admin.ads.ad_locations.{ad_id}.store
admin.ads.ad_locations.{ad_id}.create
...
You can also, use function names() like below:
Route::resource('/{ad_id}/ad_locations', PhotoController::class)->names([
'create' => 'admin.ads.ad_locations.build'
'show' => 'admin.ads.ad_locations.view'
]);
Answer For Rectified Section of the Question:
Because you are using "as" attribute in your route as well, the route names generated for you will be:
admin.admin.ads.ad_locations.store
admin.admin.ads.ad_locations.create
admin.admin.ads.ad_locations.create
You can either remove the as attribute from your route, like:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'admin.ads.ad_locations.index',
'store' => 'admin.ads.ad_locations.store',
'edit' => 'admin.ads.ad_locations.edit',
]
]);
});
});
or, you can avoid using "admin." in your routes names, like:
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'ads'], function ($router) {
Route::resource('/{ad_id}/ad_locations', AdLocationController::class, [
'names' => [
'index' => 'ads.ad_locations.index',
'store' => 'ads.ad_locations.store',
'edit' => 'ads.ad_locations.edit',
]
]);
});
});
In my Laravel 5.8 app when there are no data in session I need to redirect to some default control.
I do
return redirect()->route('admin.oauthAdminCallback/' . $form_action);
When in routes/web.php defined :
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback/{form_action}', [ 'uses' => 'Admin\EventsController#oauthAdminCallback']);//->name('oauthAdminCallback');
But I got error :
Route [admin.oauthAdminCallback/calendarActionUpdate] not defined.
If in first line $form_action has value : “calendarActionUpdate”.
Which is correct way ?
MODIFIED :
I tried this way
return redirect()->route('admin.oauthAdminCallback',$form_action);
and this way
return redirect()->route('admin.oauthAdminCallback')->with([
'form_action' => $form_action,
]);
But in both cases I do not have amy error but method was not called!
In my routes/web.php :
Route::group(['middleware' => ['auth', 'isVerified', 'CheckUserStatus'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback', [ 'as' => 'oauthAdminCallback', 'uses' =>'Admin\EventsController#oauthAdminCallback']);
// The method below is not called!
public function oauthAdminCallback()
{
session_start();
die("-1 XXZ oauthAdminCallback");
return redirect( is ignored and I can not understand why?
So you're calling the route by its name so
try this
return redirect()->route('admin.oauthAdminCallback',$form_action);
Mention your route as
Route::group(['middleware' => ['auth'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('oauthAdminCallback', [ 'as' => 'oauthAdminCallback', 'uses' => 'Admin\EventsController#oauthAdminCallback']);
});
And your callback as below
return redirect()->route('admin.oauthAdminCallback', $form_action);
Tried and tested.
I'm using Laravel 5.2 and caffeinated Modules and I'm getting this error
NotFoundHttpException in RouteCollection.php line 161:
I only get this error when I upload to my server but on my localhost I don't get an error.
I've also noticed that on my localhost I get all my routes listed but on my server I only have the home page.
My Users Module route.php
Route::group(['middleware' => 'web'], function()
{
Route::get('admin/', [
'uses' => 'UsersController#showLogin',
'as' => 'login'
]);
Route::post('admin/', [
'uses' => 'UsersController#doLogin',
'as' => 'doLogin'
]);
});
and my Users Module UsersController.php
<?php
namespace App\Modules\Users\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function showLogin(){
echo "Users Controller";
}
}
If there is anything I've missed to help with this please let me know.
This error generally shown when you access a url which is not defined in your route file. Recheck the url you are trying to access.
Your code doesn't seem to have any special error.
Add a doLogin method in your UsersController.
Besides, you can try by removing / from the route path.
I mean change this:
Route::group(['middleware' => 'web'], function()
{
Route::get('admin/', [
'uses' => 'UsersController#showLogin',
'as' => 'login'
]);
Route::post('admin/', [
'uses' => 'UsersController#doLogin',
'as' => 'doLogin'
]);
});
to this:
Route::group(['middleware' => 'web'], function()
{
Route::get('admin', [
'uses' => 'UsersController#showLogin',
'as' => 'login'
]);
Route::post('admin', [
'uses' => 'UsersController#doLogin',
'as' => 'doLogin'
]);
});
I finally found the answer. I had to delete the /storage/app/modules.json and then I ran
php artisan module:optimize
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::group(['middleware' => 'cors'], function(Router $router){
});
Route::group([
'prefix' => 'api/v1',
'namespace' => 'Api'
], function () {
Route::post('/auth/register', [
'as' => 'auth.register',
'uses' => 'AuthController#register'
]);
Route::post('/auth/login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
});
I want to add the prefix group route inside the middleware route, how can i achieve that?
You can nest route groups inside of each other. Just wrap one in the closure of the other.
Route::group(['middleware' => 'cors'], function(Router $router){
Route::group(
[
'prefix' => 'api/v1',
'namespace' => 'Api'
], function () {
Route::post('/auth/register', [
'as' => 'auth.register',
'uses' => 'AuthController#register'
]);
Route::post('/auth/login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
});
});
hey i solved this by,
Route::group([
'prefix' => 'api/v1',
'namespace' => 'Api',
'middleware' =>'cors'
],
function () {
Route::post('/auth/register', [
'as' => 'auth.register',
'uses' => 'AuthController#register'
]);
Route::post('/auth/login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
});
but the problem here is i am getting this error
XMLHttpRequest cannot load http://localhost:8000/api/v1/auth/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 500.
is there any problem with my chrome cause i heard there is plugin for cors in chrome.
Thanks :)