Share variable based on route group - laravel

I have 2 versions of a site. One is located in the root URL of the site and one is using a route prefix. They use the same resources but provide different links when accessed from the prefixed route:
Route::get('/', function(){
View::share('outgoing_url','something.com');
//regular links here
});
and a few more of the above pointing to different routes or
Route::group(array('prefix'=>'tour'), function(){
View::share('outgoing_url','somethingelse.com');
//different links here
});
View::share doesn't work since it uses whatever is assigned last so I am trying to find a solution for this problem.
Also, when I use HTML::link() in the views that go through the prefix, everything still points to the root URI of the site instead of the 'tour' prefix. Is there any way to differentiate between the two? Right now I am stuck with this problem and the only solution seems to be to make identical copies of the views and controllers responding to the routes. But that approach seems stupid to say the least.
I hope I explained the problem understandably.

HTML Macro:
<?php
HTML::macro('myLink', function($url, $title = null, $attributes = array(), $secure = null)
{
if (Request::segment(1) === 'tour')
{
$url = 'tour/'.$url;
}
return HTML::link($url, $title, $attributes, $secure);
});
?>
Usage:
HTML::myLink(...);

Just use a before filter - and set it that way
App::before(function($request)
{
if (Request::segment(1) === 'tour')
{
View::share('outgoing_url','tour.com');
}
else
{
View::share('outgoing_url','other.com');
}
});

Related

Static controller multiple routes/single controller

I tried to find an answer that does the job, but the ones I found didn't really feel right to me, I have a static part of my site which I can arrange the route normally as in the code below.
it seems so messy to repeat the same route for more 7 pages, there got to be a smarter way to do it, I need to some opinions, maybe a controller with a slug or something, but I don't quite understand how to do it with a static part of the site like this.
Thanks.
Route::get('/first/home', function()
{
return View::make('/first.home');
});
Route::get('/first/aboutus', function()
{
return View::make('/first.aboutus');
});
Route::get('/first/contact', function()
{
return View::make('/first.contact');
});
I have a feeling your coming from WP, or maybe i'm wrong. In anycase, Laravel has a diff. ballgame in terms of serving static pages.
Now after your clarification, I would recommend you group your routes, as follows:
Route::group(['prefix' => 'first'], function () {
Route::get('(.*)', 'HomeController#getPageByName');
});
in HomeController You can write the following function:
function getPageByName(Request $request)
{
$pageName = $request->path();
//since your url does not begin with the page but with 'first',
//best is to turn this into an array.
$parsedPath = explode("/",$pageName)
$data = 'any data you want to pass';
if(View::exists($pageName[1] ){
return view($pageName[1] , compact($data));
} else {
abort(404)
}
}
Essentially your url would look like this, if you want page one:
http://example.com/first/home
Now Laravel will look for views/home.blade
Let me know if this helps your issue.

How to render a cms page with default theme AND variables from controllers in OctoberCMS?

I'm wondering how I can render a view, or display a page with my default theme in OctoberCMS, via a route that executes a function in a controller.
If I have the following route:
Route::get('bransje', [
'uses' => 'Ekstremedia\Cityportal\CPController#bransje'
]);
And in my controller CPController ive tried several things, like I used to with Laravel:
public function bransje() {
$stuff = Stuff::with('info');
return View::make('cms::bransje')->with('stuff',$stuff);
}
But I cannot seem to get it to work, and I've tried to search the web, but it's hard to find answers. I have found a workaround, and that is to make a plugin component, then I can include that component and do:
public function onRun()
{
$this->eventen = $this->page['stuff'] = $this->stuff();
}
protected function stuff()
{
return ...
}
Is there any way so I can make pages without using the Cms, and that are wrapped in my default theme? I've tried
return View::make('my-theme-name::page');
and a lot of variants but no luck.
I know I can also do a:
==
public function onRun()
{
}
in the start of my page in the cms, but I'm not sure how to call a function from my plugin controller via there.
You can bypass frontend routing by using routes.php file in your plugin.
Full example in this video turotial.
If this answer can still be useful (Worked for October v434).
I have almost the same scenerio.
What I want to achieve is a type of routing like facebook page and profile.
facebook.com/myprofile is the same url structure as facebook.com/mypage
First I create a page in the CMS for each scenario (say catchpage.htm)
Then a created a catchall route at the buttom of routes.php in my plugin that will also not disturb the internal working of octobercms.
if (!Request::is('combine/*') && !Request::is('backend/*') && !Request::is('backend')) {
// Last fail over for looking up slug from the database
Route::get('{slug}/{slug2?}', function ($slug, $slug2 = null) {
//Pretend this are our routes and we can check them against the database
$routes = ["bola", "sade", "bisi", "ade", "tayo"];
if(in_array($slug, $routes)) {
$cmsController = new Cms\Classes\Controller;
return $cmsController->render("/catchpage", ['slug' => $slug]);
}
// Some fallback to 404
return Response::make(View::make('cms::404'), 404);
});
}
The if Request::is check is a list of all the resource that october uses under the hood, please dont remove the combine as it is the combiner route. Remove it and the style and script will not render. Also the backend is the url to the backend, make sure to supply the backend and the backend/*.
Finally don't forget to return Response::make(View::make('cms::404'), 404); if the resource is useless.
You may put all these in a controller though.
If anyone has a better workaround, please let us know.

Laravel: Best way to implement dynamic routing in routes.php based on environment variable?

My aim is to roll out a big re-theming / re-skinning (including new URL routing) for a Laravel v5 project without touching the existing business logic (as much as possible that is).
This is my current approach:
I placed a APP_SKIN=v2 entry in my .env file
My app\Http\routes.php file has been changed as follows:
if (env('APP_SKIN') === "v2") {
# Point to the v2 controllers
Route::get('/', 'v2\GeneralController#home' );
... all other v2 controllers here ...
} else {
# Point to the original controllers
Route::get('/', 'GeneralController#home' );
... all other controllers
}
All v2 controllers have been placed in app/Http/Controllers/v2 and namespaced accordingly
All v2 blade templates have been placed in resources/views/v2
the rest of the business logic remains exactly the same and shared between the "skins".
My question: Is there a "better" way to achieve the above?. Please note that the idea here is to affect as few files as possible when doing the migration, as well as ensure that the admin can simply change an environment variable and "roll back" to the previous skin if there are problems.
Within app/Providers/RouteServiceProvider.php you can define your routes and namespaces, etc. This is where I would put the logic you talked about (rather than in the routes file):
protected function mapWebRoutes()
{
if (App::env('APP_SKIN') === 'v2') {
Route::group([
'middleware' => 'web',
'namespace' => $this->namespace,
], function ($router) {
require base_path('routes/web_v2.php');
});
} else {
// ...
}
}
This way, you can create separate route files to make it a bit cleaner.
Aside from that, I personally can't see a better solution for your situation than what you described as I'm guessing your templates want to vary in the data that they provide, which if that is the case then you will need new controllers - otherwise you could set a variable in a middleware which is then retrieved by your current controllers which could then determine which views, css and js are included. This would mean you would only need to update your existing controllers, but depending upon your current code - this could mean doing just as much work as your current solution.
Routes pass through Middleware. Thus you can achieve this by BeforeMiddleware as follows
public function handle($request, Closure $next)
{
// Get path and append v2 if env is v2
$path = $request->path();
$page = $str = str_replace('', '', $path); // You can replace if neccesary
// Before middleware
if (env('APP_SKIN') === "v2")
{
return $next($request);
}
else
{
}
}

Wildcard routes in Laravel?

Is there a way to have a wildcard route? But only with specific names.
Eg.
I have a number of routes that lead to the same place:
/archive/gallery/1/picture/1
/masters/gallery/1/picture/1
/browse/gallery/1/picture/1
These all load up the same picture, but it would be great if I could do something like this:
Route::get('{???}/gallery/{galleryId}/picture/{pictureId}', array(
'as'=>'picture',
'uses'=>'PictureController#getPicture'
));
But only use archive or masters or browse as the wildcard.
You can't define a different controller, depending on the wildcard. You would have to define that in the controller.
Route::get('{page}/gallery/{galleryId}/picture/{pictureId}', array(
'as'=>'picture',
'uses'=>'PictureController#getPicture'
));
public function getPicture($page)
{
if ($page == "archive")
return View::make('archive');
else if ($page == "browse")
return View::make('browse');
else if ($page == "masters")
return View::make('masters');
}
be sure to place the route at the bottom of the routes file though, otherwise it will override the other routes :) as laravel uses first-in -> first->out
You may try this
Route::get('{type}/gallery/{galleryId}/picture/{pictureId}', array(
'as'=>'picture',
'uses'=>'PictureController#getPicture'
))->where('type', 'masters|browse|archive');
PictureController:
public function getPicture($type, $galleryId, $pictureId)
{
// $type could be only masters or browse or archive
// otherwise requested route won't match
// If you want to load view depending on type (using type)
return View::make($type);
}
Where you have your {???} this can simply be a regular expression.
Maybe something like this {(archive|browse|masters)}
Update: I think the above works in L3, but L4 has to be done differently
Route::get('/{variable}', function()
{
return View::make('view');
})->where('masters', 'browse', 'archive');

Is there a way to set a default url parameter for the Route class in Laravel 4?

I'm trying to set up sub-domain based routing in Laravel 4 and I've hit a bit of an annoyance...
My route group looks like this:
Route::group(array('domain' => '{company}.domain.com'), function() {
// ...
});
Which seems to work fine, however, I need to specify the company parameter for every route/url I generate. I.e:
{{ HTML::linkRoute('logout', 'Logout', ['company' => Input::get('company')]) }}
Is there any way to specify the company parameter as static/global, so it is automatically added to any links I specify, unless otherwise overwritten/removed?
Unfortunately, no (I haven't seen any evidence in the router or HTMLBuilder that you can). You could, however, make an HTML macro... Example:
HTML::macro('lr', function($link, $title) {
$company = !empty(Input::get('company')) ? Input::get('company') : "";
return HTML::linkRoute($link, $title, ['company' => $company]);
});
Then call it - instead of HTML::linkRoute, use HTML::lr('logout', 'Logout')
Just an idea.

Resources