I am trying to make a simple App in Angular , integration with Laravel 5 and facing issues while routing the application views.
Routes.php looks like as below:
<?php
Route::get('/', function () {
return view('index');
});
in Routes.php, i am handling only first time when application loads.
But after that i want to handle all routing by Angular. So, i did like this:
app.js file
var membershipModule = angular.module('membershipModule', ['ngMaterial','ngRoute']);
membershipModule.config(function($routeProvider, $locationProvider, $mdThemingProvider){
$mdThemingProvider.theme('default').primaryPalette().accentPalette('blue-grey');
$routeProvider
.when('/login', {
controller: 'LoginController',
templateUrl: 'views/login.html'
})
.when('/register', {
controller: 'RegisterController',
templateUrl: 'views/register.html'
})
.otherwise({redirectTo: "/"});
$locationProvider.hashPrefix('');
});
But when i access http://localhost:8000/#/login then it throws error that views/login.html is not found.
I tried making login.html in Laravel Default Folder as below screen
Also Tried making views/login.html in root dir as below
but none of it is working
You can put this files inside the public directory. As you can see in https://laravel.com/docs/5.3/structure#the-public-directory
Related
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', '.*');
I am trying to get a laravel-nuxt project running. I am stuck with creating route calls to my laravel backend using axios async call to serve up data to my nuxt frontend before loading the page.
I am constantly getting getting a 404 with my current laravel-nuxt setup even though I have the route defined in api.php.
I am using this as a template for the project and I have not changed anything in that template yet:
https://github.com/cretueusebiu/laravel-nuxt
So my frontend call is this here:
async asyncData ({ $axios }) {
if (process.server) {
return $axios.$get('/api/data')
.then((res) => {
this.data = res.data;
})
}
}
And my backend route is defined as follows in api.php:
Route::get('/data', 'HomeController#index');
It always gives me a 404, is there something missing that I should be aware of?
According to the Readme in the Github project you have mentioned, you have to add your routes manually to
client/router.js
Read this line under Notes and follow the structure well you'll be able to avoid this.
This project uses router-module, so you have to add the routes
manually in client/router.js.
hope this helps.
In using AngularJS on top of Laravel, assuming I have this route in Laravel:
Route::get('/admin/', function() { return View::make('pages.writer'); });
which makes a view that loads AngularJS components, including $routeProvider, and that the configuration is like so:
$routeProvider.when('/writer/:publicationName?', {
templateUrl: '/templates/writer.html',
controller: 'WriterCtrl',
caseInsensitiveMatch: true
}).otherwise({
templateUrl: '/templates/library.html',
controller: 'LibraryCtrl'
});
If I call the URL: /admin/writer/My-Favorite-Publication, I get a Laravel error because the route doesn't exist. Is there a way to tell Laravel when Angular routing takes over for a particular URL (i.e. anything after /admin/), so that I can use $routeParams.
You need to define a catch-all route that will return the base view that runs AngularJS app and let AngularJS handle the rest of the routing.
This will do the trick:
Route::get('/admin/{ignore?}', function() {
return View::make('pages.writer');
})->where('ignore', '.*');
This will make everything that starts with /admin match this Laravel route.
I am using laravel php framework for the code execution and I want angularjs to handle the routing for me. I have an example here : http://embed.plnkr.co/dd8Nk9PDFotCQu4yrnDg/preview which shows me how to swap between the pages using angularjs routing methods but they use simple .html files to render the content . It is a sample I found on the internet.
Moreover in laravel it has its own routes. How can i guide the angularjs router to call the laravel route and render the page accordingly after fetching the contents from database ? I am new to Angularjs . Thank you.
There are a couple of methods achieving the goal, but you do NOT use blade any more. Here I am just explaining the easiest way.
1. create a index.php (not index.blade.php), in your routes.php, you have:
Route::get('/', function()
{
return View::make('index');
});
It will return you the index page.
In index.php, please include
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script src="http://code.angularjs.org/1.2.3/angular-route.js"></script>
or your local dependencies.
In the public folder, you can create folder called "js", another folder called "templates".
In the "js" file, creates your "app.js", "controller.js", and etc. (Don't forget to include them in your index.php)
In the "templates" folder, you will create your template html. In your given example, they are "home.html", "about.html", "contact.html"
In the index page, you do the angular routing here.
app.js:
var app = angular.module('app', [ 'ngRoute' ]);
app.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl : 'templates/home.html',
controller : 'mainController'
})
.when('/about', {
templateUrl : 'templates/about.html',
controller : 'aboutController'
})
.when('/contact', {
templateUrl : 'templates/contact.html',
controller : 'contactController'
});
});
After getting my Laravel Homestead up and running, I'm facing a new problem.
Even after some googling, I can't find a solution for setting up routes in Laravel Homestead.
Here is my routes.php (I created files like in the comments) :
<?php
// ===============================================
// STATIC PAGES ==================================
// ===============================================
// show a static view for the home page (app/views/home.blade.php)
Route::get('/', function()
{
return View::make('home');
});
// about page (app/views/about.blade.php)
Route::get('about', function()
{
return View::make('about');
});
// work page (app/views/work.blade.php)
Route::get('work', array('as' => 'work', function()
{
return View::make('work');
}));
Route::get('users', function()
{
return View::make('users');
});
Thank you in advance for any piece of advice.
In Laravel5 the views are located in
resources/views
and not in app/views
Place all your views to resources/views and it should work.
You get the welcome page because it is shipped by default in Laravel.