Laravel 컨트롤러 하위 폴더 라우팅
저는 Laravel을 처음 사용합니다. 내 앱을 정리하고 유지하기 위해 컨트롤러를 컨트롤러 폴더의 하위 폴더에 넣고 싶습니다.
controllers\
---- folder1
---- folder2
컨트롤러로 라우팅하려고했지만 laravel이 찾지 못했습니다.
Route::get('/product/dashboard', 'folder1.MakeDashboardController@showDashboard');
내가 도대체 뭘 잘못하고있는 겁니까?
위의 Laravel 5.3의 경우 :
php artisan make:controller test/TestController
test
존재하지 않는 경우 폴더 가 생성되고 TestController
내부에 생성 됩니다.
TestController
다음과 같이 보일 것입니다.
<?php
namespace App\Http\Controllers\test;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
public function getTest()
{
return "Yes";
}
}
그런 다음 다음과 같은 방법으로 경로를 등록 할 수 있습니다.
Route::get('/test','test\TestController@getTest');
폴더에 컨트롤러를 추가합니다.
controllers\
---- folder1
---- folder2
폴더를 지정하지 않고 경로를 만듭니다.
Route::get('/product/dashboard', 'MakeDashboardController@showDashboard');
운영
composer dump-autoload
그리고 다시 시도하십시오
Laravel 5를 사용하는 경우 하위 디렉토리 내에서 컨트롤러의 네임 스페이스를 설정해야합니다 (Laravel 5는 아직 개발 중이며 매일 변경됩니다).
다음과 같은 폴더 구조를 얻으려면 :
Http
----Controllers
----Admin
PostsController.php
PostsController.php
네임 스페이스 Admin \ PostsController.php 파일은 다음과 같습니다.
<?php namespace App\Http\Controller\Admin;
use App\Http\Controllers\Controller;
class PostsController extends Controller {
//business logic here
}
그러면 이에 대한 경로는 다음과 같습니다.
$router->get('/', 'Admin\PostsController@index');
그리고 마지막으로 작곡가 나 장인이하려고하지 마세요
composer dump-autoload
또는
php artisan dump
1. 다음과 같이 하위 폴더를 만듭니다.
app
----controllers
--------admin
--------home
2. app / routes.php에서 코드 구성
<?php
// index
Route::get('/', 'Home\HomeController@index');
// admin/test
Route::group(
array('prefix' => 'admin'),
function() {
Route::get('test', 'Admin\IndexController@index');
}
);
?>
3. app / controllers / admin / IndexController.php에 sth를 작성합니다. 예 :
<?php
namespace Admin;
class IndexController extends \BaseController {
public function index()
{
return "admin.home";
}
}
?>
4. 사이트에 액세스합니다. 예 : localhost / admin / test 페이지에 "admin.home"이 표시됩니다.
추신 : 내 가난한 영어를 무시하십시오
** Laravel 5 또는 Laravel 5.1 LTS의 경우 ** Admin 폴더에 여러 컨트롤러 Route::group
가있는 경우 정말 도움이 될 것입니다. 예를 들면 :
업데이트 : Laravel 5.4에서 작동
내 폴더 구조 :
Http
----Controllers
----Api
----V1
PostsApiController.php
CommentsApiController.php
PostsController.php
PostAPIController :
<?php namespace App\Http\Controllers\Api\V1;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PostApiController extends Controller {
...
내 Route.php에서 namespace
그룹을 Api\V1
다음과 같이 설정했습니다.
Route::group(
[
'namespace' => 'Api\V1',
'prefix' => 'v1',
], function(){
Route::get('posts', ['uses'=>'PostsApiController@index']);
Route::get('posts/{id}', ['uses'=>'PostssAPIController@show']);
});
하위 폴더를 만들기위한 이동에 대한 자세한 내용은이 링크를 참조하십시오 .
방법을 찾았습니다.
/app/start/global.php에 경로를 추가하기 만하면됩니다.
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/controllers/product',
app_path().'/models',
app_path().'/database/seeds',
));
php artisan make:controller admin/CategoryController
Here admin is sub directory under app/Http/Controllers and CategoryController is controller you want to create inside directory
I am using Laravel 4.2. Here how I do it:
I have a directory structure like this one:
app
--controllers
----admin
------AdminController.php
After I have created the controller I've put in the composer.json the path to the new admin directory:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers/admin",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
Next I have run
composer dump-autoload
and then
php artisan dump-autoload
Then in the routes.php I have the controller included like this:
Route::controller('admin', 'AdminController');
And everything works fine.
If you're using Laravel 5.3 or above, there's no need to get into so much of complexity like other answers have said. Just use default artisan command to generate a new controller. For eg, if I want to create a User
controller in User
folder. I would type
php artisan make:controller User/User
In routes,
Route::get('/dashboard', 'User\User@dashboard');
doing just this would be fine and now on localhost/dashboard is where the page resides.
Hope this helps.
In Laravel 5.6, assuming the name of your subfolder' is Api
:
In your controller, you need these two lines:
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
And in your route file api.php
, you need:
Route::resource('/myapi', 'Api\MyController');
I think to keep controllers for Admin and Front in separate folders, the namespace will work well.
Please look on the below Laravel directory structure, that works fine for me.
app
--Http
----Controllers
------Admin
--------DashboardController.php
------Front
--------HomeController.php
The routes in "routes/web.php" file would be as below
/* All the Front-end controllers routes will work under Front namespace */
Route::group(['namespace' => 'Front'], function () {
Route::get('/home', 'HomeController@index');
});
And for Admin section, it will look like
/* All the admin routes will go under Admin namespace */
/* All the admin routes will required authentication,
so an middleware auth also applied in admin namespace */
Route::group(['namespace' => 'Admin'], function () {
Route::group(['middleware' => ['auth']], function() {
Route::get('/', ['as' => 'home', 'uses' => 'DashboardController@index']);
});
});
Hope this helps!!
That is how you can make your app organized:
Every route file (web.php
, api.php
...) is declared in a map()
method, in a file
\app\Providers\RouteServiceProvider.php
When you mapping a route file you can set a ->namespace($this->namespace)
for it, you will see it there among examples.
It means that you can create more files to make your project more structured!
And set different namespaces for each of them.
But I prefer set empty string
for the namespace ""
It gives you set your controllers to rout in a native php way, see the example:
Route::resource('/users', UserController::class);
Route::get('/agents', [AgentController::class, 'list'])->name('agents.list');
Now you can double click your controller names in your IDE to get there quickly and conveniently.
i had this problem recently with laravel 5.8 but i underestand that i should define controller in a right way like this below:
php artisan make:controller SubFolder\MyController // true
not like this:
php artisan make:controller SubFolder/MyController // false
then you can access the controller in routes/web.php like this:
Route::get('/my', 'SubFolder\MyController@index');
참고URL : https://stackoverflow.com/questions/18850542/laravel-controller-subfolder-routing
'IT TIP' 카테고리의 다른 글
컬이 활성화 또는 비활성화되었는지 확인하는 방법 (0) | 2020.10.17 |
---|---|
MSTest 배포 항목은 프로젝트 테스트 설정 파일에있는 경우에만 작동합니까? (0) | 2020.10.17 |
Docker-Compose는 Docker 데몬에 연결할 수 없습니다. (0) | 2020.10.17 |
Gradle을 통해 Java 클래스를 실행할 때 시스템 속성 및 매개 변수 전달 문제 (0) | 2020.10.16 |
OpenGraph 또는 Schema.org? (0) | 2020.10.16 |