Extbase UriBuilder and RealUrl on Ajax Requests - vue.js

I'm developing a TYPO3 plugin, which outputs a list of records in VueJS.
Therefor I created a controller action which returns requested records as json.
Every record has a property "uri", which holds the uri to its detail page. I generate this uri with the Extbase uriBuilder.
The first records are loaded directly within my list action, where I assign this set of records to the VueJs application directly in the frontend (v-bind:items="my_json_objects").
The next set of records will be loaded on demand by calling my API which returns the same type of records.
Problem: The uri built by uriBuilder returns a rewritten url only in the first case, when objects assigned directly to VueJS. For all items loaded by ajax calls, uribuilder returns the non-rewritten url.
Both actions calls the same method to build the uri:
$item['uri'] = $this->buildShowUri($item);
The method to build the uri:
return $this->uriBuilder
->reset()
->setTargetPageUid(56) // currently static, for testing
->setCreateAbsoluteUri(true)
->uriFor(
'show',
[
'item' => $item,
]
);
Is there a way to trigger url rewriting in this way? Do I need to register the uri somewhere to realurl?
Any hints much appreciated.

How stupid. The uribuilder works, but I forgot to enable realurl in the page type which delivers the json output.
json = PAGE
json {
config {
linkVars = L(0-4)
**tx_realurl_enable = 1**
sys_language_mode = strict
disableAllHeaderCode = 1
debug = 0
no_cache = 1
additionalHeaders {
10 {
header = Content-Type: application/json
replace = 1
}
}
}
typeNum = 129912
10 = USER
10 {
userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
extensionName = MyExt
pluginName = Plug
vendorName = Myself
controller = Events
action = apiList
switchableControllerActions {
Event {
1 = apiList
}
}
}
}

Related

Shopware 6 custom element type image not showing any data on storefront

I have created my component to add some desired config fields in Shopware 6. Everything is working fine but one problem that is image is looking as it is being saved in the administration but is not showing any src or else in dump.
And here is my dump preiew having #data null.
can anyone tell what should I do else here?
I will be very thankful.
There is a guide in the docs that explains exactly what your case is.
You can likely extend the \Shopware\Core\Content\Media\Cms\ImageCmsElementResolver and override the getType function:
public function getType(): string
{
return 'my-component-name';
}
The important part of the default ImageCmsElementResolver is the loading the media information. For that you also need in your CMS element resolver. I explain some parts of the existing ImageCmsElementResolver so you can see which steps you need:
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
// read the configuration, that is defined in the Admin JS. Likely also media for you
$mediaConfig = $slot->getFieldConfig()->get('media');
// if this config is NOT containing useful info
if (
$mediaConfig === null
|| $mediaConfig->isMapped()
|| $mediaConfig->isDefault()
|| $mediaConfig->getValue() === null
) {
// return nothing
return null;
}
// otherwise use the configured value as mediaId to load the media entry from the database
$criteria = new Criteria([$mediaConfig->getStringValue()]);
$criteriaCollection = new CriteriaCollection();
$criteriaCollection->add('media_' . $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria);
// return the criterias to execute later, when all needed entities for the CMS page are fetched
return $criteriaCollection;
}
Now the data is fetched and as next step you need to put it into a variable accessible from the Twig template. For this you write into the same CMS element resolver this:
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
$config = $slot->getFieldConfig();
$image = new ImageStruct();
// this is important for accessing data in Twig
$slot->setData($image);
// read the config again
$mediaConfig = $config->get('media');
// if the configuration looks promising
if ($mediaConfig && $config->isStatic() && $mediaConfig->getValue()) {
$image->setMediaId($config->getStringValue());
// look up the media from the entity loading step
$searchResult = $result->get('media_' . $slot->getUniqueIdentifier());
if (!$searchResult) {
return;
}
/** #var MediaEntity|null $media */
$media = $searchResult->get($config->getValue());
// if we do not have a media, then skip it
if (!$media) {
return;
}
// set the media entity to the slot data we just assigned to the slot
$image->setMedia($media);
}
}
After that you should have more info in the slot variable in Twig to embed a media.

Ktor custom feature swap url

Im trying to add a custom feature in Ktor. It's basically a url swapper (we have a scenario where domains might be changed during anytime & can't update the client everytime).
We get the swapper list available and need a CustomFeature in Ktor to swap the url based on list. However, the context.request or request.url - everything is val and Im not able to assign new url to the request.
In Retrofit, it used to work like this
if (currentUrl.contains(urlSwapper.oldUrl)) {
val newUrl = currentUrl.replace(urlSwapper.oldUrl, urlSwapper.newUrl)
val newHttpUrl = request.url.newBuilder(newUrl)!!.build()
// build a new request with the new url. replace it
request = request.newBuilder().url(newHttpUrl).build()
break
}
}
In Ktor feature, Im trying something like this
scope.requestPipeline.intercept(HttpRequestPipeline.Transform) {
val currentUrl =
context.url.protocol.name + "://" + context.url.host + context.url.encodedPath
for (urlSwapper in feature.urlSwappers) {
if (currentUrl.contains(urlSwapper.oldUrl)) {
val newUrl = currentUrl.replace(urlSwapper.oldUrl, urlSwapper.newUrl)
val newHttpUrl = Url(newUrl)
context.url(url = newHttpUrl)
break
}
}
proceedWith(subject)
}
}
Is this the right way to do this ?
Generally yes, this is the right way. I have a few recommendations:
Intercept the sendPipeline instead of the requestPipeline.
An example:
client.sendPipeline.intercept(HttpSendPipeline.State) {
context.url(url = newUrl)
}
Get rid of proceedWith(subject) call because it's redundant.
Try to use Url objects instead of strings. You can get the current URL without affecting context by cloning UrlBuilder and building Url from it: context.url.clone().build()

HTML5 history API to reduce server requests

I am trying to develop a search filter and making use of the HTML5 history API to reduce the number of requests sent to the server. If the user checks a checkbox to apply a certain filter I am saving that data in the history state, so that when the user unchecks it I am able to load the data back from the history rather than fetching it again from the server.
When the user checks or unchecks a filter I am changing the window URL to match the filter that was set, for instance if the user tries to filter car brands only of a certain category I change the URL like 'cars?filter-brand[]=1'.
But when mutiple filters are applied I have no way of figuring out whether to load the data from the server or to load it from the history.
At the moment I am using the following code.
pushString variable is the new query string that will be created.
var back = [],forward = [];
if(back[back.length-1] === decodeURI(pushString)){ //check last back val against the next URL to be created
back.pop();
forward.push(currentLocation);
history.back();
return true;
}else if(forward[forward.length-1] === decodeURI(pushString)){
forward.pop();
back.push(currentLocation);
history.forward();
return true;
}else{
back.push(currentLocation); //add current win location
}
You can check if your filters are equivalent.
Comparing Objects
This is a simple function that takes two files, and lets you know if they're equivalent (note: not prototype safe for simplicity).
function objEqual(a, b) {
function toStr(o){
var keys = [], values = [];
for (k in o) {
keys.push(k);
values.push(o[k]);
}
keys.sort();
values.sort();
return JSON.stringify(keys)
+ JSON.stringify(values);
}
return toStr(a) === toStr(b);
}
demo
Using the URL
Pass the query part of the URL (window.location.search) to this function. It'll give you an object you can compare to another object using the above function.
function parseURL(url){
var obj = {}, parts = url.split("&");
for (var i=0, part; part = parts[i]; i++) {
var x = part.split("="), k = x[0], v = x[1];
obj[k] = v;
}
return obj;
}
Demo
History API Objects
You can store the objects with the History API.
window.history.pushState(someObject, "", "someURL")
You can get this object using history.state or in a popState handler.
Keeping Track of Things
If you pull out the toStr function from the first section, you can serialize the current filters. You can then store all of the states in an object, and all of the data associated.
When you're pushing a state, you can update your global cache object. This code should be in the handler for the AJAX response.
var key = toStr(parseUrl(location.search));
cache[key] = dataFromTheServer;
Then abstract your AJAX function to check the cache first.
function getFilterResults(filters, callback) {
var cached = cache[toStr(filters)]
if (cached != null) callback(cached);
else doSomeAJAXStuff().then(callback);
}
You can also use localstorage for more persistent caching, however this would require more advanced code, and expiring data.

Wrong value binding for asp.net web API controller action paramters

I have an asp.net web api controller action (RESTful) that accepts 2 string parameters. Such a parameter can be empty. The web api action is consumed from AngularJS codes (client side Javascript) in a asp.net Razor view page.
The problem of that web api action is case 4 (see below) is never hit. In details, case 4 is supposed to run when paramter1 is passed with an empty string, and paramter2 is passed with a non-empty string. However, on running for this case and by using debugger, I find the value of paramter1 is bound to the value of parameter2, and the value of parameter2 becomes null or empty. So there is a wrong data binding with that web api action, and I do not know how to solve it. Please help. Thank you.
The web API controller action looks like:
[HttpGet]
[AllowAnonymous]
public HttpResponseMessage GetProductByParamter1AndParameter2(string paramter1, string paramter2)
{
if (string.IsNullOrWhiteSpace(paramter1) && string.IsNullOrWhiteSpace(paramter2))
{
// case 1: do something 1 ...
}
else if (!string.IsNullOrWhiteSpace(paramter1) && !string.IsNullOrWhiteSpace(paramter2))
{
// case 2: do something 2 ...
}
else
{
if (!string.IsNullOrWhiteSpace(paramter1) && string.IsNullOrWhiteSpace(paramter2))
{
// case 3: do something 3 ...
}
else // when paramter1 is empty and paramter2 is not empty
{
// case 4: do something 4 ... but this is never hit
}
}
And the custom route for that web API controller action looks like:
config.Routes.MapHttpRoute(
name: "ProductApi_GetProductByParamter1AndParameter2",
routeTemplate: "api/ProductApi/GetProductByParamter1AndParameter2/{parameter1}/{parameter2}",
defaults: new
{
controller = "ProductApi",
action = "GetProductByParamter1AndParameter2",
parameter1 = "",
parameter2 = ""
}
);
In the cshtml view page, on the client side AngularJS (Javascript codes) to consume that web API, I am coding things like:
myApp.factory('ListProductFactory', function ($http, $q) {
return {
getProducts: function (par1, par2) {
var url = _baseUrl + '/ProductApi/GetProductByParamter1AndParameter2/' + par1 + '/' + par2;
return $http({
method: 'GET',
url: url
})
}
};
});
On the controller you set the 'routeTemplate' as api/ProductApi/**GetSourceColumns**/{parameter1}/{parameter2}, it should be the name of the action GetProductByParamter1AndParameter2.
It doesn't even make sense that the url defined as something like: /ProductApi/GetProductByParamter1AndParameter2/{parameter1}/{parameter2} is still reaching the route that have been defined.
You probably have read these, but if you haven't, check out these links that explain key features of Web API
Model Validation: http://www.asp.net/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api
Routing and Action Selection: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
To solve this problem, I use query-string approach instead of segment (/) approach:
var url = _baseUrl + '/ProductApi/GetProductByParamter1AndParameter2?parameter1=' + par1 + '&parameter2=' + par2;
I spent 10 days to figure out the answer for myself. It is painful.

asp.net mvc, generate url by custom route

i have created application, where url generates depends on database values.
i parse these urls without any problem and get controller and action from database in my route handler.
but when i try to generate url, i get troubles.
in my case, it seems like:
view
#Html.ActionLink("more", MVC.Blog.Post(item.Alias)) // i use T4MVC
MyRouteConstraint
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.UrlGeneration)
{
var data = GetDataFromDbByControllerActionAndParameters(values);
if (data == null)
return false;
var valuesToRemove = new List<string>();
var path = GenerateUrlByData(data, valuesToRemove);
values.Remove("controller");
values.Remove("action");
valuesToRemove.ForEach(v => values.Remove(v)); // remove values that is already used in path
values.Add("all", path) // path = "blog/post/postalias"
return true;
}
// skipped code
}
route rule
routes.MapRoute("Locations", "{*all}",
constraints: new { all = new LocationConstraints() },
defaults: new { },
namespaces: new []{typeof(BaseController).Namespace}).RouteHandler = new LocationRouteHandler();
and as result i got url like this
localhost:8553/?Controller=Blog&Action=Post&alias=postalias
but expect like this
localhost:8553/blog/post/postalias
how can I generate url? where it should be? i think not in the constrant, but why it is invoked in this case?
In my MVC application the closest route that matches the one you have is the one below:
routes.MapRoute(
name: "MyRouteName",
url: "{SomeFolder}/{SomePageName}",
defaults: new { controller = "MyController", action = "Index" },
constraints: new { routeConstraint = new MyRouteConstraint() }
);
SomeFolder can be fixed or changed from the database while SomePageName will be changed to a different value from database. The url should be whatever URL you want to match with this route and the one that will be replaced by the value from the database. The defaults address the Controller and Action that will render the page in the end of the MVC cycle. The constraints will lead to your Match method described.
With this configuration I have URLs like www.project.com/SomeFolder/SomePageNameFromDatabase.