Can I add a Sammy route after run() - sammy.js

I have Sammy working just fine. I want to dynamically at some point addHowever I want to add a route to Sammy after the run() is invoked. Is this possible?
All my routes work that are specified before Sammy.run. However I have some dynamic routes I want to add at a later point in time.
Thanks in advance, Scott

Sure. You could define new routes after the run method. But, the initial route callback will not fire when you load the page. If you invoke the run(on dom ready) after you define your routes, sammy will fire the callback for the current route.
For example.
If you define 2 routes after run method
#/
#/List
and you hit the following url - www.mysite.com/index.html#/, the callback for #/ route will not be invoked.
I suggest calling app.runRoute('get', location.hash) or something similar to get the first route to fire.
Some code:
$(function()
{
app.run();
app.get('#/', function()
{
alert('home');
});
app.get('#/list', function()
{
alert('list');
});
app.runRoute('get', location.hash);
});
FYI Durandal 2.0 allows for child routes. Maybe have a look at.
Hope this helps

Related

Links in view files that are called through different named routes

Our team is trying to convert an old multitenant architecture over to Laravel and having some difficulty staying DRY in our routes/views.
Our account section contains multiple page views: an account dashboard, edit profile, payment methods, registrations, etc. However, we provide multiple stand-alone websites for a client, and there are multiple URL structures for the user to access their account:
In addition to the standard...
https://clientone.ourdomain.com/account/
...they can also access their account via different standalone websites:
https://clientone.ourdomain.com/event-app-one/account/
https://clientone.ourdomain.com/event-app-two/account/
All of those URLs above give them the exact same information, but with a different layout/theme applied for each standalone website. Right now the routes I set up for those above look like this:
// #### CLIENT ONE - ROOT
Route::domain('{subdomain}.ourdomain.com')->group(function() {
Route::middleware(['setTheme:clientone-account-theme'])->group(function() {
Route::get('/', 'ClientController#index')->name('index');
Route::prefix('account')->group(function() {
Route::get('/', 'AccountController#index')->name('clientone.account.show');
Route::get('/edit', 'AccountController#edit')->name('clientone.account.edit');
...
});
});
// #### CLIENT ONE - EVENT APP ONE
Route::prefix('event-app-one')->group(function() {
Route::middleware(['setTheme:clientone-eventapp-one-theme'])->group(function() {
Route::get('/', 'EventAppController#index')->name('clientone.eventapp.one.index');
...
Route::prefix('account')->group(function() {
Route::get('/', 'AccountController#index')->name('clientone.eventapp.one.account.show');
Route::get('/edit', 'AccountController#edit')->name('clientone.eventapp.one.account.edit');
...
});
});
});
// #### CLIENT ONE - EVENT APP TWO
Route::prefix('event-app-2')->group(function() {
Route::middleware(['setTheme:clientone-eventapp-two-theme'])->group(function() {
Route::get('/', 'EventAppController#index')->name('clientone.eventapp.two.index');
...
Route::prefix('account')->group(function() {
Route::get('/', 'AccountController#index')->name('clientone.eventapp.two.account.show');
Route::get('/edit', 'AccountController#edit')->name('clientone.eventapp.two.account.edit');
...
});
});
});
Those routes all currently work. We don't like including the client or app name in all of our route names because they get rather long and can't be reused, but if we take those out we found out our route URLs will override each other.
However, if we do it this way, our account views that we share across all these URLs can't use named route links for href because we don't know which one to use. For example, we can't do this in blade...
Edit Profile
...because, while that will work while at clientone.ourdomain.com/account/, it won't work while at clientone.ourdomain.com/event-app-one/account/.
Should I just not use route names for links in our views and use a relative link instead? Or am I going about my route naming all wrong? The only other thing I can think of is to copy all the view files and have one version for .com/account/ and another for .com/all-event-apps/account/ and just use a different set of route names for each. Feels wrong to duplicate essentially the same functionality in those views though.
You can use a parameter for the prefix so you are not repeating the same exact routes and creating new names for them. You can then generate routes based on those route names.
You can set defaults for parameters for the UrlGenerator to help deal with this.
I would add a middleware to your group that checks the prefix then will assign that as a default parameter on the UrlGenerator:
// instead of app-one, app-two, etc etc
Route::prefix('{app}')->middleware('handleapp')...
// handleapp middleware
Url::defaults(['app' => $request->route()->parameter('app')]);
$request->route()->forgetParameter('app'); // if you dont want this passed into controller methods
// where you need to generate a route
route('eventapp.account.edit'); // {app} is handled by the default we set so doesn't have to be passed
I would also have a middleware that will add the default parameter for subdomain as well so you don't have to pass that yourself everywhere.
Basically it all becomes dynamic and based on the current request you can have defaults setup for all these parameters so you don't have to pass them into the route helpers yourself everywhere you need a URL from a route name.
This is just the basic idea in use.

Same route on first link it works, but on second not. Laravel 5.6

Route file web.php:
Route::get('/download/received/{image_id}/{isoriginal?}', 'DownloadController#download_recv_image');
View:
<li>Download {{strtoupper($image->extension)}}</li>
<li>Download PNG</li>
Function in controller:
public function download_recv_image($image_id, $original=false){...}
This is function for download received image. When I click on first link in view route is called and function is executed. But on second link where I'am not sending second parameter then it returns me error 404 and it looks like it cant catch route.
(I have another function for download user images, with same logic for route definition in another two links and there everything works.)
I have found where the problem is.
That's because above that route I have another route called:
Route::get('download/{image_id}/{isoriginal?}', 'DownloadController#download_user_image');
I have changed second route to /received/download instead of /download/received
It's messing up because both routes have the same beginning and parameters ar messed up.

how to unset particular session in every controller except one controller

Laravel Problem: I want to unset particular session on every controller except one(specific) controller.
I am thinking of helper class. But if is there better way (like middle ware).
If is anyone have idea, please share.
Middleware is a good idea, you can use easily with your routes:
Route::group(['middleware' => 'clearSession'], function () {
// .. your controllers
});
// A controller where not clear
Route::controller('mycontroller', 'MyController');
But you can also add to your controllers constructor, and you don't have to mess with current routes, route groups.

Using pound sign (#) in Laravel routing?

Laravel has no problem routing the following URI:
$router->get('demo/toggle.html', function() {
return View::make('ng.demo.toggle');
});
However, this one won't work for some reason.
$router->get('demo#/toggle.html', function() {
return View::make('ng.demo.toggle');
});
Is there a way to make this work?
Everything behind the hashtag (#) isn't send to the server, so Laravel can't catch it when you enter it in the browser. This is where the error comes from, Laravel only gets demo.
You can try this with an existing, working route. Just write
demo/toggle.html#some_gibberish <<< will still take you to demo/toggle.html
I'm wondering why you are using '..../toggle.html' as getter, one of the benefits of (Laravel's) url rewriting is that this is avoidable. You could use only toggle instead.

emberjs should I put actions in actions hash inside controller?

"Routes and controllers that handle actions must place action handlers inside an actions hash. Even if a route has a method with the same name as the actions, it will not be triggered unless it is inside an actions hash. In the case of a controller, while there is deprecated support for triggering a method directly on the controller, it is strongly recommended that you put your action handling methods inside an actions hash for forward compatibility."
That is from the ember documentation, it sounds like I should put my actions inside the actions hash within a controller, but after I put the action inside the hash, my controller complains that Uncaught Error: Nothing handled the event 'submit'.
//this works
App.StartController = Ember.Controller.extend({
submit:function(){
alert(1);
}
});
// this complains Uncaught Error: Nothing handled the event 'submit'.
App.StartController = Ember.Controller.extend({
actions:{
submit:function(){
alert(1);
}
}
});
BTW, I am using v1.0.0rc
Just to reference #mavilein's comment, I updated my ember libraries and it's working now.

Resources