Laravel: How to Route to Public file - php

In Laravel, is it possible to redirect to a public/testFile.php, through routing?
In the application/routes.php,
Route::get('/', function()
{
//'How to point to public/testFile.php'
});
Have an existing Project, But want to do Only the NEW MODULES in Laravel. So copied the Existing project under Public/

You are completely defeating the purpose of the framework by doing this, but if you really want to...
Route::get("/", function() {
ob_start();
require(path("public")."testFile.php");
return ob_get_clean();
});
This will return the stdout output of the file. If instead you have a return value already in the script, knock out ob_start and the return call.
Redirects are done as follows:
Route::get("/", function() { return Redirect::to("testFile.php"); });

Route::get('/', function()
{
include public_path().'testFile.php';
});
If you want to Redirect then use return Redirect::to('testFile.php')
But I don't get why you want to do this weird thing.
I think you are using Laravel 3 (as you mentioned application/...), there public_path() is path('public').

Related

Redirect All URL's With A Particular Pathname To A Certain View - LARAVEL

Okay i'm running two react projects using laravel, a website and an admin section. I want both apps rendered on separate pages because their css would clash.
So in my web.php i have this Route::view('/{path?}', 'app');, but this redirects all my routes to my app.blade.php view.
I'm justing wondering if i can have a route that redirects any route with a specific pathname, let's say: mydomainname.com/admin to my admin.blade.php. Then every other route goes to my app.blade.php.
You can use Route::prefix like this:
Route::prefix('admin')->group(function () {
Route::get('users', function () {
// Matches The "/admin/users" URL
});
});
Okay i was able to pull it off. Erkan Ozkok's answer gave me a hint.
Route::prefix('admin')->group(function () {
Route::view('/{path?}', 'admin');
});
Route::any('{query}',
function() { return view('app'); })
->where('query', '.*');

Multiple Domains w/ Single Laravel Install

Sort of consolidating a few questions that are old, unresolved, and kind of in keeping with my own problem.
Routes file:
echo url()->current() ."<br>";
echo request()->getHost();
Route::domain('pro.local')->group(function () {
Route::get('/', function () {
dd('HELLO');
});
});
Route::group(['domain' => 'pro.local'], function() {
dd('PRO');
});
Route::group(['domain' => 'media.local'], function() {
dd('MEDIA');
});
Route::group(['domain' => 'software.local'], function() {
dd('SOFTWARE');
});
Route::get('/', function () {
return view('welcome');
});
Desire & environment: Three domains pro.local, media.local, and software.local all pointing to the same public folder using MAMP PRO 5.2 and Laravel 5.7. This is all I have done to the project so far.
Hypothesis: Using Route::domain or Route::group should result in returning the dd() text or the welcome template.
So far: I know the mono-repo setup I'm using works because I've had the three sites running off the mono-repo for about 3 years and can share the services and what not across projects. With that said, it's annoying to have to SSH into three separate folders to run composer update and npm update; especially when the composer.json and package.json files for each project is essentially the same...I currently use gulp to move and copy files around to keep things in sync.
Problem: No matter the domain, only PRO gets echoed.
It seems to skip the Route::domain and settle on the first Route::group, as demonstrated by moving the dd('MEDIA') call to the top.
Code inside a Route::group always gets run, as Laravel compiles the various route definitions for later use. Thus, your dd() is getting executed as Laravel builds the list of routes, short circuiting the entire process regardless of what domain you're on.
If you put each of your debugging dd calls within a Route::get('/', function () {}) inside each route group (like you do the first time with the Route::domain('pro.local') bit), you'd get the results you expected.
Route::group(['domain' => 'pro.local'], function() {
Route::get('/', function () {
dd('PRO');
});
});
Route::group(['domain' => 'media.local'], function() {
Route::get('/', function () {
dd('MEDIA');
});
});
Route::group(['domain' => 'software.local'], function() {
Route::get('/', function () {
dd('SOFTWARE');
});
});
Route::get('/', function () {
return view('welcome');
});
ALTERNATIVE: Switching them all to use Route::domain also ended up working per discovery on another forum.

Object of class Illuminate\Routing\Route could not be converted to string

I'm trying to do something very simple.
Route::get('/', function () {
return Route::view('/welcome', 'welcome');
});
I just want to it to load the welcome view and change the URI to /welcome. However, as you can see it keeps throwing the error Object of class Illuminate\Routing\Route could not be converted to string.
I haven't touched Laravel in a minute and am kind of doing a refresher and tried to set up a simple site. I may be missing something totally obvious but I have no idea what it could be.
Any help would be much appreciated.
I think your mean like
Route::redirect('/', '/welcome', 301);
Route::view('/welcome', 'welcome');
or
//one view like resources/views/welcome.blade.php
Route::get('/', function () {
return view('welcome');
});
But in fact we usually use .htaccess redirect request, because you must load all requires before do anything in framework.
You can either use
Route::view('/','welcome');
Or use
Route::get('/', function () {
return view('welcome');
});
I guess you are mixing two different syntax.
[http://www.expertphp.in/article/laravel-5-5-new-feature-route-view-and-route-redirect-method-with-example]

Laravel 5.2 session not persisting

Lately I've been working on a project in Laravel 5.2 and now I'm having problems with sessions not persisting. I've read most of the questions already asked regarding this but everyone has the same answer that I have already tried - applying web middleware.
I've read that there was a new L5.2 update where the web middleware group is already applied by default. I checked my routes with php artisan route:list and I can see that every route has only 1 web middleware applied.
I'm creating session with $request->session()->put('key', 'value') but as soon as I comment that line the session is nowhere to be seen anymore.
Edit
I want to set the session inside a controller when I visit a news page, but I tried it on a simple test route as well. Route where I set this is news/{id} and I want to use it on the front page which is in /
I wish to store recently visited pages in session so I can then show it to the user on the front page.
Session config file I left untouched. So it's using file driver
Here is a tested routes to use for your projects
Please use a middleware instead of the function in the routes file
routes.php
// Only as a demo
// Use a middleware instead
function addToSession ($routeName) {
$visited = session()->get('visited', []);
array_push($visited, $routeName);
session()->put('visited', $visited);
}
Route::get('/', function () {
addToSession('/');
return view('welcome');
});
Route::get('/second', function () {
addToSession('/second');
return view('welcome');
});
Route::get('/third', function () {
addToSession('/third');
return view('welcome');
});
Route::get('/history', function() {
return session()->get('visited');
});
The /history route will return a JSON having the history.

Combine AngularJS and Laravel Routing

I am using AngularJS and Laravel for my web app project. My routing look like this:
AngularJS:
angular.config(['$stateProvider','$urlRouterProvider',function($stateProvider,$urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: '/admin/dashboard'
});
}]);
Laravel:
Route::group(array('prefix'=>'admin', function(){
Route::get('/', ['as'=>'admin.main',function(){
return view('main');
}]);
Route::get('/dashboard', ['as'=>'admin.dashboard',function(){
return view('dashboard');
}]);
});
I am facing problem that I would need to declare route path at 2 place. One at Angular and the other one at Laravel. So, every time when I add new route or change route path, I will need to work on 2 place. This will become something tedious and hard to maintain when the app grows.
Is there anyway that I only need to set the route URL at one place, but will be effective for both?
I assume that you're building the single-page app. That means on server-side (Laravel) you need to use the same template for all GET requests, e.g.
Route::group(['prefix' => 'admin'], function() {
Route::get('(.*)', function() {
return view('dashboard');
});
});
On client-side (AngularJS) you're doing routing as described in question.
Btw, you're using wrong syntax in Laravel routing, this is incorrect:
Route::get('/', ['as'=>'admin.main',function(){
}]);
and this how it should be:
Route::get('/', ['as'=>'admin.main'],function(){
// ^
});
In Laravel 5.4 I was not able to define a route using regex as in:
Route::get('/account/(.*)', function() { return view('index'); });
Instead I had to use a parameter and use regex to ensure it captured all paths:
Route::get('/account/{path?}', function() {
return view('index');
})->with('path', '.*');

Categories