Laravel – Include external Project - php

In a Laravel project I have to include multiple projects, so that they are accessible at /example.
These projects have the structure of
/example
- index.php
- main.css
- app.js
(Usually there are more files then that.)
I have tried to use Redirect::to("example/index.php"), however this breaks all the <link>'s & <src> (where I would need to prepend /example to all of them.
This would theoretically work, however I would rather not store these files in the Laravel project itself, since they are basically self-contained projects.
What is the best way to include such external projects?

This would theoretically work, however I would rather not store these files in the Laravel project itself, since they are basically self-contained projects.
That's an excellent approach. Rather keep Laravel as Laravel and host the stuff just outside of your Laravel project.
Since you're using Apache, here's how to create a Virtual Host for that external project.
Please note - I'm assuming that your project lives in /var/www.
Decide on a URL for that project - I would use example.mylaravelproject.com. But anything will do.
Confirm the path of your project folder. For this example, we'll assume it's /var/www/example/
Run the following command (assuming you're using Ubuntu) - sudo nano /etc/apache2/sites-available/example.mylaravelproject.com.conf
Ensure the new file has the following contents:
<VirtualHost *:80>
ServerAdmin <your email address here>
ServerName example.mylaravelproject.com
DocumentRoot /var/www/example
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file
Run the following command sudo a2ensite example.mylaravelproject.com.conf
Run sudo nano /etc/apache2.conf
Make sure that this line appears somewhere in this file (preferrably in a <Directory> tag - AddType application/x-httpd-php .php
Then restart Apache by issuing the following command sudo service apache2 restart
Technically now your site has a valid vhost and should be working.
If you're doing this on a local environment and want to access your example project via the browser, you'll need to complete a few more steps:
sudo nano /etc/hosts - again, assuming that you're running Ubuntu
Add this line somewhere to your project: localhost example.mylaravelproject.com
Save and close that file. You should now be able to access that url via your browser.
If these steps don't work, it's likely that Apache isn't parsing the PHP files. If that's the case, try these links for some good answers on making Apache parse PHP:
Apache 2 server on ubuntu can't parse php code inside html file
Apache Virtual Host not parsing PHP

Related

How to setup/access page in subdirectory of separate project for a Laravel/OctoberCMS website?

I have a website running an OctoberCMS theme that I built. It's running on a server from DigitalOcean. I need to add a separate project (namely code from Matomo analytics) on the same server and access a public page (e.g. my_site.com/matomo). I'm new enough to Laravel and server configurations that I'm unsure of how I need to configure the index.php files or maybe something like .htaccess so that I can access my_site.com/matomo.
Here's my file structure
/var/www/html/
index.php (serves the pages of my project)
artisan
bootstrap/
config/
modules/
plugins/
server.php
storage/
themes/
vendor/
matomo/ (for installing the analytics for the site)
index.php
matomo.php
piwik.php
config/
a number of other files I can enumerate if necessary
I've followed the instructions from matomo but to no avail. When I try to go to my_site.com/matomo I just get a 404 from my website with my theme's formatting for it.
I know this shouldn't be hard. Thanks!
EDIT: The home page of my website is at my_site.com, as desired. The various pages are at my_site.com/page_name. This is configured fine for my purposes.
Now, for Matomo, the instructions say:
Open your FTP client and upload the Matomo files in ‘binary mode’ to the desired location on your web server. For example using the Filezilla FTP client, you can enable Binary mode transfer in the top menu Transfer > Transfer type > Binary). All files can be uploaded to a “analytics” sub-directory in your public www folder, for example http://yourdomain.org/analytics/ or you could setup Matomo in its own subdomain and upload all the files at http://analytics.example.org/
If you have SSH access to your server, you can use it instead of FTP as it is much faster: run
wget https://builds.matomo.org/matomo.zip && unzip matomo.zip
So, I used the wget option to add it to what I believe is the "public www folder", /var/www/html. So the instructions lead me to believe I can go to my_site.com/analytics/ and then view the GUI webpage for further install and setup. However, this doesn't work as it takes to a 404 page that's setup for the rest of my site. I also don't know that I'd expect it to work as none of the files or folders in Matomo are named "analytics" -- I've also tried my_site.com/matomo for the record. So, this is to say, I don't know where the Matomo page is presented.
I don't have any experience with Matomo, but from the question I understood that you want to install two software X as CMS and Y as Matomo.
Your web server(I assume Apache is the web server) will serve all request to the following path: /var/www/html/index.php. Which belong to software X. Your request my_site.com/matomo will be served to software X, and because X doesn't expect this request it will output 404 Not Found error.
The first solution: which is the bad solution and harder solution is to edit /var/www/html/index.php and combine it with Y index.php if request have matomo in the URL.
The second solution: which is the optimal and better solution is to separate X and Y. It could be done in several ways:
You can combine all X files in a folder under /var/www/html/x, and all Y files in folder under /var/www/html/y. Then you can access x using the following URL my_site.com/x and y using my_site.com/y.
If the first way in not possbile because you need to have X as root URL like my_site.com. Then you can combine all Y file under the following folder /var/www/html/matomo. Then you need to create sub domain (virtual host for Y).
Create virtual host using Ubuntu and Apache:
sudo touch /etc/apache2/sites-available/matomo.my_site.com.conf
Then edit the file using your favorite text editor I will use nano
sudo nano /etc/apache2/sites-available/matomo.my_site.com.conf
and past the following code:
<VirtualHost *:80>
ServerAdmin admin#my_site.com
ServerName matomo.my_site.com
DocumentRoot /var/www/html/matomo/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Change my_site.com to your domain.
Then you need to enable the virtual host
sudo a2ensite /etc/apache2/sites-available/matomo.my_site.com.conf
Then restart apache to see the result.
sudo systemctl restart apache2
Your matomo will be under the following URL matomo.my_site.com
If you are not using Ubuntu or Apache, tell me what are you using to update my answer.
You don't need to fix anything in index.php. Analytics counters can be added to the layout (~/themes/mytheme/layout/mylayout.htm) of the page between the
<head>
...
</head>
or
<body>
...
</body>
according to the instructions of your meter
See https://github.com/octobercms/october/issues/1615. You need to add the folder you want to allow access to as an exclusion to the October CMS .htaccess, and then also disable the line that disables running any PHP file other than index.php.
So replace RewriteEngine On with
RewriteEngine On
# Allow access to Matomo
RewriteRule ^(matomo)($|/) - [L]
and then comment out RewriteCond %{REQUEST_FILENAME} \.php$ so it becomes # RewriteCond %{REQUEST_FILENAME} \.php$.

Laravel basic routing returns 404

I've recently started learning Laravel but there is one problem which I don't know why it occurs. My current Laravel project is located at wamp/www/codebright. When I access localhost/codebright/public I see the welcome page to Laravel.
When I create a simple routing:
Route::get('my/page', function()
{
return "Harro world";
});
and trying to access:
localhost/codebright/public/my/page it returns with 404 error, not even with Laravel error. I've also tried to access: localhost/codebright/my/page and still.
However, if I type in CMD php artisan serve and open a server on 8000 port and then access:
localhost:8000/my/page it works just fine. I would like to know why my first method without the artisan command didn't work.
Thanks in advance!
Note
It seems like that if you have XAMPP installed, none of the problems mentioned above and in the answer comment section are occurring. Basically, if you are using XAMPP, you most likely won't get any error and the program will work just fine.
It is indeed possible to do what you want but if you're not using artisan serve you must have a webserver set up correctly. From your original post you obviously have a webserver set up as you get the welcome page, but it looks to me like one of the following:
You don't have the .htaccess file in place
Your base vhost (or Apache config if not using a vhost) on your web server setup does not AllowOverride All (which is required to allow .htaccess files to work)
You don't have mod_rewrite turned on
You should check these out. As a minimum, Laravel requires a way to turn URIs that don't exist as real files (my/page) into something it can fake a page for. This pretty much requires the use of mod_rewrite and an .htaccess file to specify the rules.
Explanation of the difference between using Apache and artisan serve: artisan serve does not use a 'dumb' webserver like Apache and instead uses a webserver built into PHP which has knowledge of how to handle 'non-existing' URIs that you browse to, which is why you don't need mod_rewrite and the .htaccess file.
Have you created virtual host for your laravel project?
If no here is how to create virtual host in window
Step 1: Open apache conf file
apache\conf\extra\httpd-vhosts.conf
Add below code in last line.
<VirtualHost *:80>
ServerName www.mark-thomas.loc
DocumentRoot E:\wamp\www\codebright\public
SetEnv APPLICATION_ENV "development"
<Directory E:\wamp\www\codebright\public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
E:\wamp\www\codebright\public is your laravel app path, you can replace it with your folder path.
Step 2:
Open-> Windows\System32\drivers\etc\hosts
Add your ip address in list of IP address
192.168.1.231 www.laravel.loc
Restart your apache server hit http://www.laravel.loc/my/page. Now you will see your message!

XAMPP localhost returns object not found after installing Laravel

After much faffing about I got laravel to work with XAMPP. However, I can't seem to access directories in the htdocs folder via localhost now. Attempt to access a file returns Object not found! along with The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
The changes I made to get laravel working seems like a blur now. The only thing I remember doing is editing the hosts file to enable the virtual host to work by adding 127.0.0.1 laravel.dev in the hosts file (using a mac btw). I also added a virtual host to the httpd-vhost.conf file.
I did undo the above changes but it didn't make a difference.
Any thoughts on whats gone wrong?
Thanks.
Dispelling Confusion
Just thought I'd clarify what my experience is. Before installing laravel 4 I could access all my projects with localhost/someProjectName but now it fails.
I'm trying to identify what change caused this behaviour. Btw, I have no problems accessing my laravel project (my mapping allows me access to it via laravel.dev)
Look into your /etc/httpd.conf file and see if you have Virtual hosts activated.
If so, check your etc/extra/httpd-vhosts.conf file. You might have set up a VirtualHost already.
EDIT: I have found that once you turn on Virtual Hosts, XAMPP needs a default VirtualHost for your basic htdocs files
Try adding a default VirtualHost (at the bottom of the file) like so:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Applications/XAMPP/htdocs"
<Directory "/Applications/XAMPP/htdocs">
Options Indexes FollowSymLinks Includes execCGI
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
It sounds to me like it is a problem with the way your directories are configured. Be sure to create a Virtual Host (if you're using Apache) that points to the public folder within your application directory.
For example, if your Laravel project is under /Applications/XAMPP/htdocs/Laravel then set up a Virtual Host like this:
<VirtualHost *:80>
DocumentRoot /Applications/XAMPP/htdocs/Laravel/public
# Other directives here
</VirtualHost>
Additionaly, if you're working with PHP >= 5.4, SSH into the application folder and run
./artisan serve
(Be sure that your PHP executable is in your PATH variable). Then go localhost:8000 and you should have your application running.
Running this command runs the PHP built-in webserver and saves you the trouble of configuring virtual hosts.
In your Apache's http.conf, find the DocumentRoot line and add the subdirectory /public on the end.
Once that is done and you've restarted Apache, you'll be able to access everything which is contained within your htdocs/public folder (including subdirectories of that folder), along with any routes you've defined in Laravel.
Laravel is designed to be set up this way as to protect the code and files by not having them in the folder which is served out to the web.
I have same issue and found that there are miss configuration in vhost. So that's are correct configuration.
in etc/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/laravel-master/public"
ServerName laravel.local
ErrorLog "logs/laravel-master-error.log"
<Directory D:/xampp/htdocs/laravel-master/public>
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
In the hosts file
127.0.0.1 laravel.local
I don't know if you're running L4 or L3. However, launch CLI and
$ cd ./path/to/project
Then for L4:
$ php artisan serve
For L3:
$ php -S localhost:8000 -t public
Now you can go to localhost:8000 and see your application.
You're having problem because the object really doesn't exist in your htdocs directory. You don't have to append xampp after localhost or 127.0.0.1 because xampp will treat it as an object or a folder under htdocs.
if you want to access your blog, make sure you have a blog folder under htdocs and put in your URL localhost/blog

Point to a virtual directory

I deployed an application on Heroku and I used a folder to place all my files inside thus now my application is only accesible from:
http://myapp.heroku.com/app/
Is it possible to create a virtual root to point
http://myapp.heroku.com -> http://myapp.heroku.com/app/ ?
Something similar to Apache VirtualHost?:
<VirtualHost 10.1.2.3>
ServerAdmin webmaster#host.foo.com
DocumentRoot /www/docs/host.foo.com
ServerName host.foo.com
ErrorLog logs/host.foo.com-error_log
TransferLog logs/host.foo.com-access_log
</VirtualHost>
Thanks in advance.
Yes you can configure the apache as well, however, this needs some changes on your system.
I've compiled a blog post recently that shows this (as the last part), it also shows how you can compile your own PHP extensions for heroku:
PHP on Heroku, again (by hakre; 20 May 2012)
It basically works by extending the standard configuration with your additional settings in another file. Look for the Configure the Webroot section, that's where it starts:
Now comes the next tricky part that is specifying the webroot. Specifying the webroot needs a little bit more work and background information. The CVBacklogs applications webroot in the git-tree is src/app/public. For Heroku, by default, the webroot is the root of the git-tree. That directory is internally mapped to /app/www btw. So what this needs is to create a so called Procfile that starts a sh-script each time the Heroku app web-node starts. That script then modifies the Apache configuration and includes your own config which is setting the webroot to /app/www/src/app/public. So we create the procfile, a config directory, the script and the Apache configuration. Ready?
You can't do anything with Apache / Nginx configuration on Heroku - these are all beyond your control. You could do some kind of php based redirect in the root folder to the /app folder or alternatively rejig the repo so app is the top level.

Netbeans and Zend-Framework

I have just started using the Zend-Framework. On advice of a friend I use NetBeans for PHP development with Zend. I have installed NetBeans and referenced the Zend-Framework under Tools>Options>PHP>Zend, registered it since I am using a version newer then 1.10.
Under PHP>General I have inluded the Zend-library path as a Global-Include-Path.
Since I've read that the beginners tutorial provided on the website (http://framework.zend.com/manual/en/learning.quickstart.create-project.html) has some errors and since it does not use NetBeans I started with this video tutorial: http://netbeans.org/kb/docs/php/zend-framework-screencast.html
Creating the project as a Zend-Framework Project worked just fine, all default folders and files are created. However, when I run the project with this defauilt setup the browser should diplay the index.php provided by the Framework under localhost/quickstart, instead of that it just displays a listing of the files of directories:
Index of /quickstart
Parent Directory
.zfproject.xml
application/
docs/
library/
nbproject/
public/
tests/
I suppose there is something wrong with the configuration of the apache server but the video screencast did not mention any needed configuration when using netbeans.
I am using xampp and one of the things that might be the problem is the httpd.conf file as described in the tutorial (http://framework.zend.com/manual/en/learning.quickstart.create-project.html) since no NameVirtualHost-propperty is defined there and no VirtualHost configured. However I didn't want to change the httpd.conf without knowing if that is the problem.
Also adding a line "127.0.0.1 quickstart.local" to the hosts file turned out to be impossible under Windows 7, so in case this is actually neccesary, I would apreciate any help.
Thanks,
Lukas
Have you set the DocumentRoot in the apache httpd-vhost.conf to the public folder?
All you should need to do is add public to the end of your current document root...
Also to change the vhosts file in windows 7 you need to find notepad in the start menu -> all programs -> accessories
Right click and run as Administrator
File -> open -> C:\Windows\System32\drivers\etc\hosts
Now add your records and save.
Opening as Administrator allows the save to attually save the changes.
Ironically, I just went through all of this yesterday. First, what happens when you type: localhost/quickstart/public, into your browser instead of: localhost/quickstart ? The controller is accessed via the index action and .htaccess file inside that directory.
Second, in regards to editing the hosts file... I have windows 7 as well, and all i did to find it was type "system32" in the Search Programs and files, then follow the path to the hosts file. I was able to edit it using notepad and it worked great!
there are two steps to configuring the vhost after that. You will need to uncomment a line in the httpd.conf file under the apache folder, remove the # from httpd.conf:
#Include conf/extra/httpd-vhosts.conf
Then go to the extras folder and add the following to the bottom of your httpd-vhosts.conf file like so:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.localhost
DocumentRoot "C:/path_to_local_webroot/quickstart.com/public"
ServerName quickstart.com
ServerAlias quickstart.com
ErrorLog "logs/quickstart.com-error.log"
CustomLog "logs/quickstart.com-access.log" common
</VirtualHost>
For your hosts file, just add one line to the bottom:
127.0.0.1 quickstart.com
Then restart your server and you should be good to go.

Categories