@rodrigo.pedra Need your insights on this! :)
Routing between Angular and Laravel
Hello We have started working on a angulara project(Not sure if the term is right or in use). We are facing challenges in routing back and forth using laravel and angular. Use Case: We have a multiple page application where we are using Angular(Not using angular cannot be a solution). Our one app view has the inception section i.e user login and registration. The 2nd app has the users dashboard and the 3rd app view has a website editor.
Now while loading app our laravel route is invoked at first.
routes.php
Route::get('/','WelcomeController@index');
Route::get('dashboard','DashboardController@dashWelCome');
WelcomeController@index
public function index()
{
return view('start.index');
}
Now, when we have to switch from the login app to the dashboard app(after user logs in), we need to load the app first in order to send request as till the app does not load not request would be served.
$scope.doLogin = function(customer) {
Data.post('login', {
customer: customer
// customer: sanitizeCredentials(customer)
}).then(function(results) {
Data.toast(results);
if (results == "success") {
// $location.path('dashboard');
window.location.href = path+"dashboard";
}
});
};
This is what we do: return success from laravel auth method and in the callback hit the dashboard URL(above) ,this loads the dashboard and then we can further perform operations. But instead we want the user to go to dashboard/profile after login,but as the app view is not loaded we do not get the result and the url just redirects to root like localhost/dashboard/profile.
Same issue in the reverse manner,when user logs out we want to send him to login app view but as we are in the dashboard app we are redirected to laravel route which does not have any view attached, as the views are attched to angular.Not sure if I can return a parameter with logout route and catch in angular to load the login app again and then go ahead. Please suggest.
I'll assume the following, correct me if I am wrong:
- On Laravel's route
/dashboardyou return the HTML with proper angular bindings (e.g.:ng-app="...") for bootstraping an angular app. - Inside this HTML you have an angular app that uses
ngRoutemodule to load internal views. - Upon loading the app you want to load the angular route/view
dashboard/profileand let angular load the partial in ang-viewdirective somewhere.
So try this:
- After logging the user in, redirect him/her to the
dashboardroute:
.then(function(results) {
Data.toast(results);
if (results.data == "success") {
window.location = "./dashboard"; // this will make a request to laravel's router
}
});
- In the app loaded in the
dashboardadd thisrun(...)block
var app = angular.module('dashboard', ['ngRoute']); // module definition
/* ... other relevant code ... */
// this will run when the angular app is loaded for the first time
app.run(['$location', function ($location) {
$location.path( '/profile' ).replace(); // "replace()" will replace the page in the browser's history
}]);
By your description I think this will nail it.
Please or to participate in this conversation.