Link to a another page from a dynamic page in laravel - php

I created a dynamic page by taking data from the database.
Example: example.com/post/SE12
This /SE12 is dynamically created. Now I have a button in this page where I want to edit the post.
example.com/post/SE12/edit.
How can I link a button to this page using laravel? and how can I route this page and call it in controller?
In route.php
Route::resource('posts','PostsController');
Route::get('/posts/{code}', [ 'as'=>'post-show', 'uses'=>'PostsController#show']);

routes.php:
Route::get('post/{code}/edit', [ 'as'=>'post-edit', 'uses'=>'PostsController#edit']);
Controller:
public function edit($id)
{
echo $id; //this will give the code. e.g. SE12
}
View:
some title
N.B. you are resource controller and manual mapping at the same time. stick to either one as much as possible. otherwise routes will get conflict.

Related

How do I effectively record page views on php [ laravel ]

Good day,
I am trying to record page views/visit on my page..
currently I added a helper function on my controller
my helper function:
function record_user_click($type=null,$item_id=null)
{
$referer = (array_key_exists('HTTP_REFERER',$_SERVER)) ? $_SERVER['HTTP_REFERER']:'direct_access';
\DB::table('click_statistics')->insert([
['user_id' => Auth::id(),'type'=>$type,'item_id'=>$item_id,'referer'=>$referer]
]);
}
and on my product controller I the helper function:
public function product_view($id){
record_user_click('product',$id);
-- controller codes here
}
this function records every visit/view but if I refresh the page fast, it also records them, which will add to its view statistics on the table.
my question is, how can I possibly make it more reliable something like stackoverflow's views function.
If you want to register to browse pages when it is fully loaded, you need to do this using javascript. Wait for fully loaded pages and send ajax with data to your backend portion and then register the user

how to routes localhost url in Laravel 5.2

I am working with laravel application. in my application I have controller function to view form as following.
public function create()
{
return view('projects.new');
}
when view projects/new.blade.php file it is contain form. in this form fill and save it is generated new project in project table. now I need after save this form next page redirect to the collaborator.blade.php file in the same projects folder. and local host url as this
localhost:8000/projects/10/collaborators
this is my controller method
public function projectcollaborators()
{
return view('projects.collaborators');
}
and routes
Route::get('projects/{projects}/collaborators', [
'uses' => '\App\Http\Controllers\ProjectController#projectcollaborators',
]);
how can I manage controller and routes to success above requirements?
You should have a Route::post in your route file to capture the form submit,
say something like Route::post('projects', 'ProjectController#create');
The function that returns the view should be called index with a route like this: Route::get('projects', 'ProjectController#index');
In the create function you can use Input() to grab the information submitted by the form.
After grabbing the information you need you can then add the details to your projects table using Eloquent.
e.g.
$project = new Project();
$project->name = 'Name'
$project->fields = 'Text'
$project->save();
After inserting and saving the details into your database you can do a redirect to projects/ID/collaborators route using the redirect function.
You will also need to create a route for projects/ID/collaborators so you dont get a 404...
e.g. Route::get('projects/{id}/collaborators', 'ProjectController#collaborators');

On redirecting to a View, slide it to the specific part of the page using #id - Laravel 5.3

I have a route which redirects to a controller which tells view to show. But how can I go to a particular part of the page. Let me Explain:
I am using fullpage.js library to create a page breakpoints.
for example: /home#firstPage , /home#secondPage and so on. When I am redirected to /home I would like to go to a particular section of the page. All I need is a way to enter #3rdPage in url.
Where and How do I put this to see it action?
my Routes
Route::get('/home_done', ['as' => 'home_done','uses' => 'HomeController#home']);
my Controller
public function home()
{
return view('home_done');
}
I would like to do something like:
return view('home_done#3rdPage');
You should setup a new function and route to redirect the users to correct page after executing your logic:
public function home()
{
return view('home_done');
}
public function step_3()
{
// do something and redirect
return redirect('home_done#3rdPage');
}
Using view don't work since you are loading a file, not the page.
You need change your behavior in JS, when a user comes to your page without a # consider it equal of #firstpage.
and in your html manage all over pages by properly generate urls with #what_you_need.
in your blade example:
firstpage
//OR
firstpage //because this is the same for your JS
secondpage
or blade syntax
firstpage
//OR
firstpage //because this is the same for your JS
secondpage

Controller action not supporting the view

It's my first time I am using zend studio. I am working on existing php application and successfully running on server. I am trying to add the new view in the application under specific folder. For this first I am appending the controller to add a new action and created the new view with same name. Unfortunately the controller action is unable to display the view.
Here is my controller code.
public function testAction() {
mysqli_close($this->db);
}
I am not passing any variables in my controller action because my view is php free code. I don't need any variable from action on my view. I am redirecting to view using this code.:
url(array('controller'=>'dashboard', 'action' => 'test', 'brand'=>$this->escape('nrgrs')),'default',true) ?>">
Unfortunately my redirection fails when I click on the link. Any idea?
Thanks,
Ashnav

Load another view when click on a image

I am using codeigniter for a project. I set controller A as my default controller in routes.php. So in this controller A it will load my home page.
If I want to access another view from the homepage for example about or contact, how do I go about doing it??
Example:
<a href="http://yoursite.com/yourcontroller/yourfunction">About Page
Now you have yourcontroller that has the yourfunction in it, from within this function you can load the view for about page, like:
function yourfunction() {
$this->load->view('your_about_view_page_name");
}
And that shall work. Hope it helps

Categories