Prefix route laravel and show in blade - laravel-5.1

I want to config prefix for url.File route.php is something like this
Route::group(['prefix' => 'speech', 'as' => 'speech'], function()
{
Route::resource('user', 'UserController', ['as' => 'speech.user']);
});
And in file blade, linkRoute is something like this
{{ Html::linkRoute('speech.user.index') }}
That's code will show error : Route [speech.user.index] not defined.
What can I do?

Try this
{{ url('/speech/user/index') }}

Related

How to create custom urls with dashes in Yii2?

I am working with advanced project application and trying to add URL rules in Yii2 to handle custom URLs with dashes.
What I want to do is to change the URL from
http://www.example.com/post/details?url=example-post-title
To
http://www.example.com/example-post-title
I have below configuration which works fine when the URL parameter does not have dash (exampleposttitle).
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
// ...
'<url:\w+>' => 'post/details',
],
],
You need to fix your regexp, since \w+ does not allow dashes:
'<url:[\w-]+>' => 'post/details',

Yii2 UrlManager + wrong generate route

I have some problem - urlrules is correct but generated url from yii\bootstrap\Nav not correctly ->:
{domain}/armory/search?server=Lorem+ipsum
but this url working too -> {domain}/armory/search/Lorem+ipsum
'search/<server>' => 'search/index'
url rule ^
protected function addUrlManagerRules($app)
{
$app->urlManager->addRules([new GroupUrlRule([
'prefix' => $this->id,
'rules' => require __DIR__ . '/url-rules.php',
])],true);
}
why this not generate url like {domain}/armory/search/Lorem+ipsum
'url' => ['/armory/search', 'server' => 'Lorem+ipsum'],
You need to use pattern in the format <ParamName:RegExp>
Read here
try something like this in your urlManager section rules.
'site/search/<server:\w+>' => 'site/search'
Url::to(['site/search', 'server' => 'lorem_ipsum']) could be display site/search/lorem_ipsum

Pjax in Yii 2 with POST request

How can I use Pjax in Yii 2 with POST request?
I tried to do it like that but page restarts anyway:
<?php Pjax::begin(['id' => 'some-id', 'clientOptions' => ['method' => 'POST']]);?>
...
some content
...
<?=Html::a('', ['cart/cart'],
['class'=>'close1',
'data' => [
'method' => 'post',
'params' => [
'idCartToDelete' => $product->idCart,
],
]
]
)
?>
...
some content
...
<?php Pjax::end(); ?>
You're close to solving your issue. To your Html::a parameters add:
'data-pjax' => 0,
And you will have a link that does not redirect to another page.

Silex : The token storage contains no authentication token

When trying to check whether the user is authenticated or not in layout
{% if is_granted('IS_AUTHENTICATED_FULLY') %}
<p>Username: {{ app.user.username }}</p>
{% endif %}
I am getting an error as
Twig_Error_Runtime in Template.php line 304:
An exception has been thrown during the rendering of a template ("The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.") in "layout.html" at line 39.
This is the configuration of the security firewall. I need to only allow logged in users to access the website.
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'dev' => array(
'pattern' => '^/(_(profiler|wdt)|css|images|js)/',
'security' => false
),
'login' => array(
'pattern' => '^/login$',
),
'secured' => array(
'pattern' => '^.*$',
'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
'logout' => array('logout_path' => '/logout'),
'users' => $app->share(function() use ($app) {
// Specific class App\User\UserProvider is described below
return new App\User\UserProvider($app['db']);
}),
),
'unsecured' => array(
'anonymous' => true,
)
),
'security.access_rules' => array(
// You can rename ROLE_USER as you wish
array('^/.+$', 'ROLE_USER'),
array('^/login$', 'SS'), // This url is available as anonymous user
)
));
Any ideas to fix this is welcome.
Thank you
Since the error message says that the error happens in layout.html, I'm guessing it is used on every page even the ones like /login that is not behind a firewall. The error is caused by calling is_granted when not behind a firewall.
So there are a few options:
Use a separate layout for login page that does not call is_granted
Check if there is an existing security token before calling is_granted
Option 1 should be obvious so not going into more detail with that.
With option 2, you can do something like this to check for existing security token:
{% if app.security.token is not null and is_granted('IS_AUTHENTICATED_FULLY') %}

AngularJS routes can't be recognized on manual refresh

Hi I followed a tutorial: http://omarriott.com/aux/angularjs-html5-routing-rails/
Here's my manual.js.coffee.erb
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
#= require_tree ./templates
#Bento = angular.module('bento', ['ngRoute', 'ngResource', 'ngSanitize', 'ui.bootstrap', 'infinite-scroll'])
# Pre-cache templates
Bento.run ($window, $templateCache) ->
templates = $window.JST
for fileName of templates
fileContent = templates[fileName]
$templateCache.put(fileName, fileContent)
# Note that we're passing the function fileContent,
# and not the object returned by its invocation.
#= require_tree ./controllers
#= require_tree ./services
#= require_tree ./directives
My routes.js.coffee.erb
#Bento.config ["$routeProvider", ($routeProvider) ->
$routeProvider.when("/",
controller: "ActivitiesController"
templateUrl: "<%= asset_path('templates/user/activities.html.haml') %>"
redirectTo: (current, path, search) ->
if search.goto
"/" + search.goto
else
"/"
).when("/projects/activities",
controller: "ProjectAllActivitiesController"
templateUrl: "<%= asset_path('templates/projects/activities.html') %>"
redirectTo: "/example"
).when("/projects/:id",
controller: "ProjectCtrl"
templateUrl: "<%= asset_path('templates/projects/list.html') %>"
redirectTo: (current, path, search) ->
if search.goto
"/projects/:id" + search.goto
else
"/projects/:id"
).otherwise redirectTo: "/"
]
And lines that I added to my config/routes.rb file
match "api" => proc { [404, {}, ['Invalid API endpoint']] }, via: [:post]
match "api/*path" => proc { [404, {}, ['Invalid API endpoint']] }, via: [:post]
match "/*path" => redirect("/?goto=%{path}"), via: [:post]
Since I'm using rails 4, I need to add a via
# sign is adding whenever I refresh my root path localhost:3000/ but it's not working in nested URLs like localhost:3000/projects/1 it's still reading my routes for RAILS.
PS: Whenever i refresh localhost:3000/projects/activities for example, it's still reading my rails routes instead of going to localhost:3000/example.

Resources