NotFoundExeception in RouteCollection when using multiple route groups - laravel

I define in RouteServiceProvider.php this map implementation:
public function map(Router $router, Request $request)
{
$locale = $request->segment(1);
$this->app->setLocale($locale);
$router->group(['prefix' => '{lang}'], function($router) {
require app_path('Http/routes.php');
});
}
In app/routes.php Ι also define a route group
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function (){
//code
);
Now when Ι try to access to admin/xxx it show me this exception
NotFoundHttpException in RouteCollection.php line 161:
I try to put the definition of the first route group in routes.php but that didn't work.
How can Ι solve the problem?

The group who defined into the RouteServiceProvider.php comes before the group who defined into the routes.php
So, you should access your route like this:
xxx/admin

Related

Why does middleware always fires?

After logging in, my app always goes to the dashboard as intended.
But even after clicking other directories, it always goes to the dashboard.
As I find out, the middleware named RedirectIfAdmin always fires.
I am using hesto/multi-auth.
RedirectIfAdmin:
public function handle($request, Closure $next, $guard = 'admin')
{
if (Auth::guard($guard)->check()) {
return redirect('myapp/dashboard');
}
return $next($request);
}
On my admin route file:
Route::group(['prefix' => '/myapp', 'as'=>'myapp.'], function () {
Route::resources([
'user' => '\App\Http\Controllers\UserController'
]);
});
So if I go to myapp/user/ i am always redirected to myapp/dashboard.
I know that it was the code insde the RedirectIfAdmin that is firing when I go to myapp/user because if I change it to myapp/dashboard123 then it goes to that url everytime i browse myapp/user, myapp/user/create, myapp/user/123, myapp/user/123/edit, etc.
Any thoughts?
While calling your myapp/user/ try to call it with
project-url/admin/myapp/user
if you are calling with route then use like
admin.myapp.user
Step 1:
Inside your RouteServiceProvider.php
In that inside map function add this line:
$this->mapWebRoutes();
$this->mapAdminRoutes();
Step 2:
Inside RouteServiceProvider class add this function:
protected function mapAdminRoutes()
{
Route::group([
'middleware' => ['web', 'admin', 'auth:admin'],
'prefix' => 'admin',
'as' => 'admin.',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/admin.php');
});
}
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
Step 3:
Run below command in your project
php artisan optimize:clear
Check the __construct() on the user controller see if you referenced it there

Laravel except a single route from auth middleware

I have a route group which is protected by the auth middleware, and inside of this group I want to except one route. But this route is also located in another route group. So when I try to move it out of this group, it is not working.
How can I fix this problem, and except a UrlProfile function from auth middleware?.. I am using Laravel 5.1
Route::group(['middleware' => 'auth'], function () {
// some other routes ...
Route::group(['namespace' => 'Lawyer'], function() {
Route::get('profile/{name}', 'ProfileController#UrlProfile');
}
};
Can you try this?
Route::group(['namespace' => 'Lawyer'], function () {
Route::get('profile/{name}', 'ProfileController#UrlProfile');
Route::group(['middleware' => 'auth'], function() {
..
..
..
)};
)};
If I understood your problem correctly, This should also work.
You can add this in your controller.
You can insert the name of your function in the except section and it will be excluded from the middleware. [Reference]
public function __construct()
{
$this->middleware('auth')->except(['yourFunctionName']);
}

Laravel : How to define route start with prefix # like medium.com/#{username}?

How to define route in laravel , which start with prefix # in web.php
like myapp.com/#username
use route group() to define prefix as -
Route::group(['prefix' => '#username'], function () {
Route::get('/','TestController#test');
});
You can access this url as www.base_url.com/#username/
If you want to set username dynamically then you could this-
Route::group(['prefix' => '#{username}'], function () {
Route::get('/','TestController#test');
});
And in your controller-
public function test($username)
{
echo $username;
}
I have got my answer .
Route::group(['namespace' => 'User','prefix' => '#{username}'],
function () {
Route::get('/','UserController#get_user_profile');
});
in User Controller
public function get_user_profile($username){
$user=User::where('slug',$username)->first();
return response()->json($user);
}

Laravel redirect route with a parameter from controller

I'm trying to redirect a route from a controller function after a form submit process in Laravel 5.4 as it is said in link below
https://laravel.com/docs/5.4/redirects#redirecting-named-routes
Route;
Route::group(['middleware' => ['api'], 'prefix' => 'api'], function () {
Route::post('doSomething', 'Page#doSomething');
});
Route::post('/profile', function () {
//..
});
Controller;
public function doSomething(Request $request){
return redirect()->route('profile', ['id'=>1]);
}
When I try to redirect I get this error.
InvalidArgumentException in UrlGenerator.php line 304: Route [profile]
not defined.
I have searched several times about redirection but I got similar results.
Any suggestions ? Thank you.
route() works with named routes, so name your route
Route::post('/profile', function () {
//..
})->name('profile');

laravel 4 page is not working for a newbie

I am totally new for laravel and trying to create a view for about us page.
But it is not working right when I am setting this in a route file.
Here is my HomeController functions:
public function showWelcome()
{
return View::make('hello');
}
public function showAboutus()
{
return View::make('about.about');
}
And here is the routes file:
Route::get('/', 'HomeController#showWelcome');
Route::get('about', 'HomeController#showWelcome');
.... 'HomeController#showWelcome' ...
should be
.... 'HomeController#showAboutus' ...
On the About route.
Plus the second parameter is an array so use
Route::get('/', array('uses' => 'HomeController#showWelcome'));
Route::get('about', array('uses' => 'HomeController#showAboutus'));

Resources