Thursday, April 14, 2016

Install multiple laravel projects in subfolders without subdomain

Leave a Comment

I already tried to search for this issue, but it's all different from mine, so I'm posting this here. I'm trying to create a webserver using nginx to host multiple laravel projects in subfolders. It's my labs server. So I'd like to have my projects like this:

  • domain.com/project1
  • domain.com/project2
  • domain.com/project3

I'm copying the following nginx location block for each project (i don't know what's happening here, I just copied from the internet and it worked):

location ^~ /project1/ {         alias /home/web/project1/public;         try_files $uri $uri/ @project1;      location ~ \.php {         fastcgi_pass                    unix:/var/run/php5-fpm.sock;         fastcgi_index                   index.php;         include                         /etc/nginx/fastcgi_params;         fastcgi_param                   SCRIPT_FILENAME "/home/web/project1/public/index.php";     }  }  location @project1 {      rewrite /avm/(.*)$ /project1/index.php?/$1 last; } 

And RESTful routes in my laravel app like this:

/* |-------------------------------------------------------------------------- | 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::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');     }); }); 

Although it seems to work (the page appears, etc) when it goes into bussiness logic (save to database, etc.) it appears to have many bugs. For example when I try to create a new employee in url http://domain.com/project1/administracao/funcionarios it gives me the error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '/administracao/funcionarios' in (it's kinda prepending some url routes)

And when I setup a subdomain like project1.domain.com everything works fine. But I don't want to create a subdomain for each project, I want it to work in subfolders url. Is it possible?

3 Answers

Answers 1

I think the problem might be in your nginx.conf file. Try this:

location ^~ /project1 {         alias /home/web/project1/public;         try_files $uri $uri/ @project1;      location ~ \.php {         fastcgi_pass     unix:/var/run/php5-fpm.sock;         fastcgi_index    index.php;         include          /etc/nginx/fastcgi_params;     }  }  location @project1 {     rewrite /project1/(.*)$ /project1/index.php?/$1 last; } 

Also, please be sure that /home/web/project1/ is outside of your web root.

This being said, it is really not recommended to run Laravel in a subfolder. Much easier in a subdomain.

I got the basic idea for this suggestion from this gist.

Answers 2

Not completely sure about the solution, but I think you should try these ones:

  • Firstly: setting the url properly in config/app.php file.
  • Secondly: reviewing public/web.config file. The problem might be these configuration overwriting your nginx. consider changing <action type="Rewrite" url="project1/index.php" /> and see what it returns.

In last instance, var_dump the model and look for where it comes from.

Answers 3

have you tried this configuration ?

https://gist.github.com/tsolar/8d45ed05bcff8eb75404

I will test tomorrow as soon I have time to replicate your situation in my env.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment