Laravel 4 - Route to subdomain except www - php

Currently I have a routing group to subdomains and another routing group to the main site, like this:
Route::group(array('domain' => '{subdomain}.mysite.dev'), function() {
// Subdomain routes
Route::get('/', array('as' => 'home', 'uses' => 'SubdomainController#showHome'));
});
// Main
Route::get('/', array('as' => 'home', 'uses' => 'PublicController#showHome'));
It's working fine so far for both 'mysite.dev' and for 'stuff.mysite.dev' or any other subdomain.
The problem is that it assumes 'www' (as in 'www.mysite.dev') as a subdomain and I need it to be interpreted as the main site.

Thanks to itachi response, I came up with this:
Route::pattern('subdomain', '^((?!www).)*$');
Adding this line to the top of routes.php, makes the 'subdomain' match anything except 'www'.
Final form:
Route::pattern('subdomain', '^((?!www).)*$');
Route::group(array('domain' => '{subdomain}.mysite.dev'), function() {
// Subdomain routes
Route::get('/', array('as' => 'home', 'uses' => 'SubdomainController#showHome'));
});
// Main site routes (works for mysite.dev and www.mysite.dev)
Route::get('/', array('as' => 'home', 'uses' => 'PublicController#showHome'));

Route::pattern('subdomain','dev|test|mobile'); <---- add your subdomains
Route::group(array('domain' => '{subdomain}.mysite.dev'), function() {
// Subdomain routes
Route::get('/', array('as' => 'home', 'uses' => 'SubdomainController#showHome'));
});
// Main
Route::get('/', array('as' => 'home', 'uses' => 'PublicController#showHome'));
just add the 1st line and you are done!

1) You could use .htaccess redirect www to non-www
2) check http://laravel.com/docs/routing for Regular Expression constraints, so at the end of the route you add ->where('subdomain', '!www'); ( not tested )

Related

Route login not defined (Multi-auth)

I have problem with multi-auth. Postman says error:
problem:
Route [login] not defined.
I understand it doesn't know which one to route?
I try to make laravel passport api so I dont need any automatic redirections.. I try to create multi-auth.
Like Admins, Stylists, Freelancers, clients..
Every route have own login and registration and other routes..
I tryed to ungroup but this isn't good solution..
Route::group(['middleware' => ['json.response']], function () {
Route::get('/freelancer/{profile}', 'API\FreelancerController#profile'); // Guests can also see profiles..
Route::group(['prefix' => 'admin', 'namespace' => 'API', 'middleware' => 'auth:admin'], function() {
Route::post('/login', 'AdminController#login');
});
Route::group(['prefix' => 'freelancer', 'namespace' => 'API', 'middleware' => 'auth:freelancer'], function() {
Route::post('/login', 'LoginRegisterController#login');
Route::post('/register', 'LoginRegisterController#freelancerRegister');
});
Route::group(['prefix' => 'stylist', 'namespace' => 'API', 'middleware' => 'auth:stylist'], function() {
Route::post('/login', 'LoginRegisterController#login');
Route::post('/register', 'LoginRegisterController#stylistRegister');
});
Route::group(['prefix' => 'client', 'namespace' => 'API', 'middleware' => 'auth:client'], function() {
Route::post('/login', 'ClientController#login');
Route::post('/register', 'ClientController#clientRegister');
});
});
If I'm http://api.mywebsite.com/freelancer/register then I can register freelancer account..
Or If I'm http://api.mywebsite.com/admin/login then I can only login to admin dashboard.
POSSIBILITY 1
Please check what route you are targetting when creating a login request from POSTMAN.
I believe you are using a route like
http://api.mywebsite.com/login
whereas you should be using one of the following:
http://api.mywebsite.com/admin/login
http://api.mywebsite.com/freelancer/login
http://api.mywebsite.com/stylist/login
http://api.mywebsite.com/client/login
POSSIBILITY 2
If above isn't the case and you are targetting a good route then check your method, it should be POST, by default POSTMAN uses GET method when you initialize a request.
However, chances for this mistake is less as it would give rather METHOD NOT EXIST error than ROUTE NOT FOUND but it's worth a try.

Laravel 5 new routes don't work

I'm starting with laravel 5 framework and I have a problem with the routes.
In the last few days, the routes worked correctly, but today I added a new route and it doesn't work anymore.
I have these routes
Route::get('url/create', 'UrlController#create');
Route::get('url/bulk', 'UrlController#bulk_view');
Route::post('url/bulk', ['as' =>'url/bulk', 'uses' => 'UrlController#bulk']);
Route::get('url/bulk_metrics', 'UrlController#bulk_metrics_view');
Route::post('url/bulk_metrics', ['as' =>'url/bulk_metrics', 'uses' => 'UrlController#bulk_metrics']);
Route::post('url/create', ['as' =>'url/create', 'uses' => 'Urlcontroller#store']);
Route::post('url/update/{id}', ['as' =>'url/update', 'uses' => 'Urlcontroller#update']);
Route::get('urls', ['as' =>'url/list', 'uses' => 'Urlcontroller#index']);
Route::get('url/{id}', ['as' =>'url/show', 'uses' => 'Urlcontroller#show']);
Route::post('url/delete/{id}', ['as' =>'url/delete', 'uses' => 'Urlcontroller#destroy']);
All work correctly, but I added this new route
Route::post('urls/filter', ['as' =>'url/filter', 'uses' => 'Urlcontroller#filter']);
and I call it like this
{!! Form::open(array('route' => 'urls/filter', 'method' => 'POST')) !!}
I tried php artisan route:clear, php artisan route:cache and php artisan route:list, and the new route appear in the list:
POST | urls/filter | url/filter | App\Http\Controllers\Urlcontroller#filter | web,auth |
The other routes work correctly and I think that it is a cache problem, because if I change the url/create to url/create2, and I change it in the template to url/create2 it doesn't work.
Thanks in advance to all
You should use it as url/filter
{!! Form::open(array('route' => 'url/filter', 'method' => 'POST')) !!}
because you're naming it like this:
'as' =>'url/filter'
Or remove 'as' =>'url/filter' part from route. In this case name of your route will be urls/filter and not url/filter.

Named Routes Conflict Laravel 5.2

I am creating a Multilingual Laravel 5.2 application and I am trying to figure out how to change language when I already have the content.
routes.php
...
Route::get('home', [
'as' => 'index',
'uses' => 'SiteController#home'
]);
Route::get([
'as' => 'index',
'uses' => 'SiteController#inicio'
]);
...
I have SiteController#home and SiteController#inicio. So I change session('language') in SiteController#change_language like:
...
public function change_language ($lang){
session(['language' => $lang]);
return redirect()->action(SAME NAMED ROUTE, DIFFERENT LANGUAGE);
}
...
So, When I click on a button with
English
from /inicio (SiteController#inicio) I should be redirected to the same named route (SiteController#home) so I can check the language and display appropriate content.
Any ideas of how to get the named route or something helpful?
Thank you :)

Laravel-5 Redirect Loop

My routes.php like this:
Route::get('deneme/{test}', 'TestController#index');
Route::get('send', 'MailController#index');
Route::get('elfinder/tinymce4', 'Barryvdh\Elfinder\ElfinderController#showTinyMCE4');
Route::group(['middleware' => ['auth', 'perm'], 'prefix' => 'admin'], function(){
Route::get('/', ['as' => 'admin', 'uses' => 'Admin\DashboardController#index']);
Route::resource('kategori', 'Admin\KategoriController');
Route::resource('icerik', 'Admin\ContentController');
// Property Routes
Route::resource('property', 'Admin\PropertyController');
Route::post("property/lang", ['uses' => 'Admin\PropertyController#langAdd', 'as' => 'admin.property.lang_add']);
Route::get("property/lang/{id}", ['uses' => 'Admin\PropertyController#langDelete', 'as' => 'admin.property.lang_delete']);
Route::get('users/add', 'Admin\UsersController#add');
Route::get('content/add', 'Admin\ContentController#add');
});
Route::get('admin/login',['as' => 'admin.get_login', function(){
return view("admin.login");
}]);
Route::post('admin/login', ['as' => 'admin.do_login', 'uses' => 'Auth\AuthController#postLogin']);
Route::get('admin/logout', ['as' => 'admin.logout', 'uses' => 'Auth\AuthController#getLogout']);
When I send request to site.dev/admin the browser say:
This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS
I have delete rotation and leave blank only PHP tag available but this url -site.dev/admin- return same error. I tested 2 browsers, Chrome and Firefox. I think this error stems from browser-cache and installed Opera browser -not installed before- but still get same error.
I solved this problem. Under the public folder exist a folder with name is admin. So i changed my rotation like this:
Route::group(['middleware' => ['auth', 'perm'], 'prefix' => 'adminpanel'], function(){
Route::get('/', ['as' => 'admin', 'uses' => 'Admin\DashboardController#index']);
Route::resource('kategori', 'Admin\KategoriController');
Route::resource('icerik', 'Admin\ContentController');
// Property Routes
Route::resource('property', 'Admin\PropertyController');
Route::post("property/lang", ['uses' => 'Admin\PropertyController#langAdd', 'as' => 'admin.property.lang_add']);
Route::get("property/lang/{id}", ['uses' => 'Admin\PropertyController#langDelete', 'as' => 'admin.property.lang_delete']);
Route::get('users/add', 'Admin\UsersController#add');
Route::get('content/add', 'Admin\ContentController#add');
});
And the new url rotation doesn't conflict any folder under the public folder.
Good works.

Laravel - Get route name in filter

How can I get current route name in filter? I tried use Route::currentRouteName(); but it's null.
Route::filter('belongsToUser', function(){
dd( Route::currentRouteName() );
exit;
});
Route looks for example:
Route::get('/openTicket/{id}', array('before' => 'auth|belongsToUser', 'uses' => 'MyController#MyAction'));
Your route isn't named, so it's no surprise the route name is null. You need an as parameter.
Route::get('/openTicket/{id}', array(
'as' => 'yourRouteName',
'before' => 'auth|belongsToUser',
'uses' => 'MyController#MyAction'));
http://laravel.com/docs/routing#named-routes

Categories