在子文件夹中使用 Laravel 时如何编写路由

Posted

技术标签:

【中文标题】在子文件夹中使用 Laravel 时如何编写路由【英文标题】:How to write routes when using Laravel in subfolder 【发布时间】:2016-08-26 16:34:38 【问题描述】:

我想在我的域 mydomain.com/project1 的子文件夹中提供我的 Laravel 应用程序。我应该如何编写我的 routes.php?我正在使用这样的东西:

<?php

/*
|--------------------------------------------------------------------------
| 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::get('/', ['middleware' => 'auth','uses' => 'HomeController@index'])->name('home');

// Authentication
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@authenticate');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Administração
Route::group(['prefix' => 'administracao', 'middleware' => 'auth'], function() 
    Route::resource('filiais', 'FiliaisController');
    Route::resource('precos', 'PrecosController');
    Route::resource('funcionarios', 'FuncionariosController');
    Route::resource('cargos', 'CargosController');
    Route::resource('vendedores', 'VendedoresController');
);

// Comercial
Route::group(['prefix' => 'comercial', 'middleware' => 'auth'], function() 
    Route::resource('clientes', 'ClientesController');
    Route::resource('fichas', 'FichasController');
);

// Operacional
Route::group(['prefix' => 'operacional', 'middleware' => 'auth'], function() 
    Route::resource('agenda', 'AgendaController');
    Route::resource('os', 'OsController');
    Route::resource('ambientes', 'AmbientesController');
    Route::resource('processos', 'ProcessosController');

    Route::get('relatorios', 'RelatoriosController@index');
    Route::get('relatorios/word/os', 'RelatoriosController@word');
    Route::get('relatorios/excel/os', 'RelatoriosController@excel');
    Route::get('relatorios/create', 'RelatoriosController@create');

    Route::group(['prefix' => 'processo', 'middleware' => 'auth'], function() 
        Route::get('create', 'ProcessoController@create');
        Route::get('index', 'ProcessoController@index');

        Route::post('os/parse', 'ProcessoController@parse');

        Route::get('os', 'ProcessoController@principal');
        Route::match(['get', 'post'], 'os/detalhe', 'ProcessoController@detalhe');
        Route::get('os/duplicidades', 'ProcessoController@duplicidades');
        Route::get('os/restantes', 'ProcessoController@restantes');
        Route::match(['get', 'post'], 'os/auditoria', 'ProcessoController@auditoria');
        Route::match(['get', 'post'], 'os/operadores', 'ProcessoController@operadores');
        Route::match(['get', 'post'], 'os/divergencia', 'ProcessoController@divergencia');
        Route::match(['get', 'post'], 'os/finalizar', 'ProcessoController@finalizar');
        Route::get('os/excluir/setor', 'ProcessoController@destroy');
    );
);

我得到NotFoundHttpException in RouteCollection 任何网址。我还尝试添加路由前缀Route::group(['prefix' =&gt; 'subfolder'], function () ...,但仍然无法识别

编辑

我没有编辑我的 htaccess,它是:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %REQUEST_FILENAME !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %REQUEST_FILENAME !-d
    RewriteCond %REQUEST_FILENAME !-f
    RewriteRule ^ index.php [L]
</IfModule>

【问题讨论】:

您是否在project1 文件夹 安装了应用程序? 是的..我复制了project1文件夹中的公用文件夹和我在public_html之外复制的应用程序的其余部分,然后我从公用文件夹编辑了index.php以匹配框架文件夹 试试 mydomain.com/project1/public 它不起作用..NotFoundHttpException再次 我的解决方案仅适用于主页.. 其余的不起作用.. 它重定向到 auth/login 但我所做的任何事情都会从那里得到同样的错误 【参考方案1】:

转到 index.php 并更改

#From this
require __DIR__.'/../bootstrap/autoload.php';
#To this
require __DIR__.'/../[framework-folder]/bootstrap/autoload.php';


#From this 
$app = require_once __DIR__.'/../bootstrap/app.php';
#To this
$app = require_once __DIR__.'/../[framework-folder]/pulcro/bootstrap/app.php'

.htacces

RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %REQUEST_URI !^
RewriteRule ^(.*)$ /$1 [L]

【讨论】:

我用我的 .htaccess 更新了我的问题.. 是否正确?我的意思是.. 我应该删除RewriteCond %REQUEST_FILENAME !-d 吗?还有什么?

以上是关于在子文件夹中使用 Laravel 时如何编写路由的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 自动路由,如 Codeigniter - 自定义解决方案

如何在laravel路由文件中编写动态url方法?

如何laravel视图里访问其他控制器的路由

学妹问我如何使用laravel写后台侧边栏模板,我笑了笑,然后给了她本秘籍。

学妹问我如何使用laravel写后台侧边栏模板,我笑了笑,然后给了她本秘籍。

学妹问我如何使用laravel写后台侧边栏模板,我笑了笑,然后给了她本秘籍。