Angular 4 routes don't work from laravel public folder - laravel

I have a laravel json api being consumed by an angular 4 app,when it comes to deployment,I'm using forge with the contents of the dist folder in laravel's public folder,but I can't refresh any of my angular routes because laravel doesn't recognise them,they only work when navigating normally(clicking and so on) not when I enter them into the address bar of the browser or refreshing as I said.How can I solve this problem?

I have a same problem, so I figured out a way for over 3 hrs.
Here is my way.
1. Build angular 4 project first(ng build)
2. Copy all files except for 'index.html' in dist folder to public directory of laravel project.
3. replace content of resources/views/welcome.blade.php with angular4 index.html
4. add following codes to routes/web.php
Route::get('/{any?}', function () {
return View('welcome');
});
Please let me know if you have a problem still.

Related

Adding subdomain in Laravel 9

For some reason adding subdomain route isn't working and keeps on returning my homepage instead of the page I need. I'm using Laravel, Inertiajs Vue in my app.
Here is my route:
Route::domain('webshopview.localhost:8000')->group(function () {
Route::get('/webshopview', function () {
return Inertia::render('Products/Index');
});
});
I want to be able to access it as webshopview.localhost:8000 but every time I try to visit this route it returns my app home page. If I do this in normal route like the below example, it works like a charm.
Route::get('/webshopview', function () {
return Inertia::render('Products/Index');
});
What is missing to create a subdomain for group of routes? Why it keeps returning the app homepage ignoring the subdomain
My app works locally on 'http://localhost:8000/' and 'http://127.0.0.1:8000/' and it works like that by default without me doing anything. So all I want is to add subdomain to specific routes so that I can access like this 'example.localhost:8000' or 'example.127.0.0.1:8000'. The end result is to have the route displayed like this after deployment 'https://www.subdomain.domain.com'
I even tried this and it didn't work:
Route::get('/productview', function () {
return 'First sub domain';
})->domain('productview.localhost');
now accessing 'http://productview.localhost/' returns 'This site can’t be reached' error.
I had to manually add the routes in my etc/hosts file on windows and restart the entire machine for it to take effect.

Path for images for Laravel project from cpanel

I have a problem with displaying pictures in View for my project in Laravel..destination path in my application where the photos were saved is: public_path (/ images) but recently I put the project on the server in cpanel, and to get rid of / public from the url, I used a google article, and I made a folder called laravel outside the public_html folder, in which I keep the application files and in public_html I keep only the files that were in the public folder of the application. In View I display the pictures as follows: "{{asset ('images /'. $ Img-> file_name)}}" .. Only it is no longer displayed. Pictures are saved but no longer displayed. I mention that it is saved in the Laravel folder which is outside the public_html folder..and I think that public / images was created automatically there..I would like to know if anyone has faced such a thing and what is the solution. thank you
I solved the problem like this:
In AppServiceProvider in register() I put this code:
$this->app->bind('path.public', function () {
return base_path('../public_html');
});
and now I have public_html/images and there my images are saved..and the rest of myy app, outside public_html folder in a different folder named laravel_project
In Laravel 8, I solved it adding this to the /app/Providers/AppServiceProvider.php in register method:
public function register()
{
$this->app->bind('path.public', function () {
return base_path('../public_html');
});
}
But in order to create the symlink from the "php artisan storage:link" command, I also had to add this code to /bootstrap/app.php, just after the last singleton:
$app->bind('path.public', function () {
return base_path("../public_html");
});
Finally I used this code to get the image that will be sent to my blade template:
$image=Storage::disk('public')->url('path_to_image');

Laravel Route to Standalone WebApp

I am trying to build a portal in Laravel to serve some other, standalone web apps (not built in Laravel), but I am struggling to find out how to route to these apps if I want to place them outside the public folder.
In the past, I would use (temporary) symlinks for this kind of things, but I was wondering if Laravel provides another solution.
So, I have a folder:
module
module/index.php
module/js/whatever
module/css/whatever
module/img/whatever
and I want a route /modules/1 to link to index.php in the module-folder in such a way that the resources in this folder (js/css/img) are also accessible.
Any suggestions?
You can include other PHP files with require_once.
web.php
Route::any('/webapp/{assets?}', 'WebAppController#index');
WebAppController
class WebAppController {
public function index(Request $request) {
require_once '../module/index.php';
if ($request->assets) {
// check from session if user is logged in
// require asset as well
// (or download them https://laravel.com/docs/5.5/responses#file-downloads)
}
}
}
http://php.net/manual/en/function.require-once.php

Working with Laravel routes

I built my application using Angularjs on the frontend and Laravel 5 at the backend, however my main issue now is routing, when the page is loaded initially I set it to return my angular.php view I even added some code to catch all routes and return that view for me.
This does not work in all cases:
routes.php
Route::any('{url?}', function($url) {
return view('angular');
})->where(['url' => '[-a-z0-9/]+']);
Example of a URL that works with this is:
http://localhost:8000/tickets/events/catgeories/
Example of a URL that does not work with this is:
http://localhost:8000/tickets/events/Musical/Some-event-name
By "not working" I mean Laravel throws a NotFoundHttpException. What I am thinking right now is the above route can't go past three levels/parameters as in /level-1/level-2/level-3.
What am I doing wrong here?
Maybe because second URL has uppercase characters?

Laravel 4 installation on server not working

Hello I have installed laravel 4 on my server, the installation was done from cpanel , I get the default welcome message of Laravel on this link http://www.mysite/laravel/public
I added a controller and a view and I tried to run the url for that new controller and I get a Error 404 - Not Found
The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.
This is what I did after laravel was installed :
TestController.php
<?php
class TestController extends BaseController {
public function index()
{
return View::make('index');
}
}
routes.php
Route::get('/Test', function()
{
return View::make('index');
});
views/index.php
<h1>Test</h1>
am I doing something worng?
here is my folder structure :
I had a similar problem when deployed my project on a host the first time.
You should put all the content of your public folder in the pre-existing public or public_html folder and the whole app folder in the root folder of the server.
Then you change app/config/app.php to tell the new path of app to laravel, and change your .htaccess file to point the document root to the new public folder.
This worked for me, if you need additional details, ask.

Resources