1
votes

1- ROUTES:

  • Created the following file in "Routes" directory "frontend.php" with the following route.
<?php

use Illuminate\Support\Facades\Route;

Route::get('about-us', [App\Http\Controllers\front\PostController::class, 'about']); 

2- Updated "App/providers/RouteServiceProvider.php, in function public function boot(){ with the new route file.

                // For front route
                Route::middleware('web')
                ->namespace('/App/Http/Controllers/front')
                ->prefix('front')
                ->group(base_path('routes/frontend.php'));
                //

2- Views

  • in "Resources/view" created frontend folder with index.blade.php

3- Controllers

  • in "App/Htp/Controllers" view created front folder with PostController.php
<?php

namespace App\Http\Controllers\front;
use App\Http\Controllers\Controller; 
use App\Models\front\Post;
use Illuminate\Http\Request;


class PostController extends Controller
{
    public function about()  
    {
        dd("About ");
    }
}

Now when i write in the browser address bar this url: http://127.0.0.1:8000/front/about-us i receive this error

ErrorException
Undefined offset: 0
http://127.0.0.1:8000/front/about-us

Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
C:\xampp\htdocs\laravel\last-dev\vendor\laravel\framework\src\Illuminate\Collections\Collection.php:1424

C:\xampp\htdocs\laravel\last-dev>php artisan route:list

+--------+-----------+----------------------------+-------------------+------------------------------------------------------------------------+------------+
| Domain | Method    | URI                        | Name              | Action                                                                 | Middleware |
+--------+-----------+----------------------------+-------------------+------------------------------------------------------------------------+------------+
|        | GET|HEAD  | /                          |                   | Closure                                                                | web        |
|        | GET|HEAD  | api/user                   |                   | Closure                                                                | api        |
|        |           |                            |                   |                                                                        | auth:api   |
|        | GET|HEAD  | front/about-us             |                   | App\Http\Controllers\front\PostController@about                        | web        |
|        | GET|HEAD  | front/{name?}              |                   | App\Http\Controllers\front\PostController@index                        | web        |
|        | GET|HEAD  | front/{name?}/{id?}        |                   | App\Http\Controllers\front\PostController@atest                        | web        |
|        | GET|HEAD  | home                       | home              | App\Http\Controllers\HomeController@index                              | web        |
|        |           |                            |                   |                                                                        | auth       |
|        | POST      | login                      |                   | App\Http\Controllers\Auth\LoginController@login                        | web        |
|        |           |                            |                   |                                                                        | guest      |
|        | GET|HEAD  | login                      | login             | App\Http\Controllers\Auth\LoginController@showLoginForm                | web        |
|        |           |                            |                   |                                                                        | guest      |
|        | POST      | logout                     | logout            | App\Http\Controllers\Auth\LoginController@logout                       | web        |
|        | GET|HEAD  | newspaper                  | newspaper.index   | App\Http\Controllers\NewspaperController@index                         | web        |
1
just to point out, a backslash is used in a namespace not a forward slash, so '/App/Http/Controllers/front' should be '\App\Http\Controllers\front' - ths
both work same in laravel @ths - Psycho
@Psycho didn't know about this, maybe laravel do some replacement under the hood as PHP will see it as a syntax error. Thanks for the info btw. - ths
Use uppercase-first in your namespace definition. I cant see any other issues, but I wonder which guide you are following - Flame
It seems weird what you are doing! For clarification, are you new to Laravel? or just trying some other patters in laravel? like repository patern? @Mansour - Psycho

1 Answers

0
votes

you need to update routeServiceProvider.php file


Route::prefix('front')
    ->middleware(['web','front','auth:front'])
    ->as('front.')
    ->namespace($this->namespace)
    ->group(base_path('routes/frontend.php'));

After than you need to update routes/frontend.php

use App\Http\Controllers\front\PostController;

Route::group(['namespace' => 'Front'],function(){
    
    Route::get('about-us',['PostController::class','about']);
});

Try this way hopefully, this will work.