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', '.*');
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'm a beginner in Laravel and I'm having some difficulties with routes. Actually, I'm having difficulties with a lot of things in Laravel, some of which I've managed to wrap my head around (such as migrations, seeding and authentication) but this is one of the most basic ones.
I've been creating routes based on the one that comes with Laravel. However, after much googling, something seems off. I'm not sure this is how it should be done.
My current web.php file looks like this:
Route::get('/', function () {
return view('pages.home');
});
Route::get('/about', function () {
return view('pages.about');
});
Route::get('/login', function () {
return view('login');
});
Route::get('/student', function () {
return view('profiles.student');
});
Route::get('/professor', function () {
return view('profiles.prof');
});
Route::get('/profadmin', function () {
return view('profiles.profadmin');
});
Route::get('/ident', function () {
return view('pages.ident');
});
// Authentication
Auth::routes();
Route::post('/login', function () {
return view('pages.ident');
});
Route::get('logout', 'Auth\LoginController#logout');
// Route::get('/home', 'HomeController#index')->name('home');
// Route::get('/ident', 'HomeController#ident')->name('ident');
//
// Route::get('/aluno', 'HomeController#aluno')->name('aluno');
//
// Route::get('/ident', 'HomeController#ident')->name('ident');
Also, certain pages should only be viewed by authenticated users and I'm having a hard time understanding how exactly that is done and how the routes should reflect that.
I'm sorry if this is simple stuff, but this is my first time using a PHP framework. Any help will be much appreciated.
lets suppose you want to protect the about route
then in the web.php file, replace your about route with this:
Route::get('/about', function () {
return view('pages.about');
})->middleware('auth');
now anyone hits /about and not logged in, it will be redirected to /login
if you want to know more about authentication, Laravel documentation really the best place for you:
https://laravel.com/docs/5.5/authentication#protecting-routes
First if you are beginner you should read Laravel documentation & Laracasts
In your routes you trying to show only views
Route::get('/about', function () {
return view('pages.about');
});
In Laravel 5.6 you can do it like this
Route::view('/about', 'viewName');
If I will create some application on Laravel (for example it will be project.com) and in this same application I will develop admin area (with ACL, users management, etc.). Can I use it like project.com for front-side but backoffice.project.com for admin area in same application?
Thanks.
You can maintain both applications in the same Laravel project and use grouped routes and filter your routes by domain.
Route::group(['domain' => 'backoffice.project.com'], function () {
// your BACKEND routes...
});
Route::group(['domain' => 'www.project.com'], function () {
// your FRONTEND routes...
});
You can complement the route comportment with middleware too.
// in this case all backend routes will be passed to auth middleware.
Route::group(['domain' => 'backoffice.project.com', 'middleware' => 'auth'], function () {
// your BACKEND routes...
});
Important:
Observe that the Laravel documentation talk about Sub-Domains Routing. In this case, the approach of the documentation is the use of dynamic subdomains, as can be seen in the following example.
Route::domain('{account}.myapp.com')->group(function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});
In this case, {account} is a route parameter that can be used inside of the route group.
You can see (and read) more about Laravel routes here: https://laravel.com/docs/5.5/routing
Yes, you can group routes by domain.
Since Laravel 5.3 you can group them like this:
Route::domain('project.com')->group(function () {
// Your frontend routes
});
Route::domain('backoffice.project.com')->group(function () {
// Your backend routes
});
Before Laravel 5.3 you can group them like this:
Route::group(['domain' => 'project.com'], function () {
// You frontend routes
});
Route::group(['domain' => 'backoffice.project.com'], function () {
// You backend routes
});
I have this route.php:
Route::group(['prefix' => 'v3/page1'], function()
{
Route::get('page1', 'TestController#page1');
});
Route::group(['prefix' => 'v4/page1'], function()
{
Route::get('page1', 'TestController#page1');
});
As you can see, there are 2 groups that have the same routes. The only difference is that the prefix is slightly different for each group.
I need a way to pass data from route to controller. In this case Im only interested in passing the "v3" or "v4"-string from route to controller.
I have read a little about before_filter. But Im not sure if it is the right way to go.
I can imagine that a solution could be to extract the url (maybe in the constructor for the controller) and from there understand if the prefix is v3 or v4. But I wonder if there is a better way, more a best practice. Maybe something with before_filter?
You can try something like:
Route::group(['prefix' => '{version}/page1'], function(){
Route::get('page1', 'TestController#page1');
})->where('version', 'v[3|4]');
In your controller you can get the version by $request->version
Route::group(['prefix' => '{v}/page1'], function()
{
Route::get('page1', 'TestController#page1');
});
& in your method
public function page1($v) {}
Read more from https://laravel.com/docs/5.2/routing#route-parameters
I would write it like this
Route::group(['prefix' => '{version}'], function()
{
Route::get('page1', 'TestController#page1');
});
I wouldn't pass in 'page1' in prefix, as it would mean page1 will show up twice in route.
In 'page1($version)' method you should be able to get the version.
I haven't tested this though.
In Laravel PHP Framework, How can I apply a filter on all routes/pages of the website except one specific route/page?
Update:
It will be great if there is a way other than (route groups)?
use route groups
// unsecured routes.
Route::get('/', 'UserController#getLogin');
Route::group(array('before' => 'yourFilter'), function()
{
// secured by filter `yourFilter`.
Route::controller('route1', 'XxxController');
Route::post('user/save', function() {
// content
});
Route::get('user', 'UserController#getUser');
});